2013-03-19 106 views
2

我是WPF和XAML的新手,但仍然在閱讀有關這個​​問題時很棘手。我想在鼠標懸停上更改的是前景顏色,但標籤位於按鈕內容的內部。一切都應該按照風格完成,沒有C#在鼠標懸停上更改標籤前景,但標籤位於按鈕內容的內部

<Button Name="Home_Button" Height="50" VerticalAlignment="Top" Click="Home_Click" Background="Gray" Foreground="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0"> 
    <DockPanel> 
     <Image Source="Images/icons/home.png" HorizontalAlignment="Left" Margin="-90,0,0,0" /> 
     <Label VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,-90,0" Foreground="LightGray" FontSize="12">Home</Label> 
    </DockPanel> 
</Button> 

我的風格的設置是這樣的:

<Style TargetType="{x:Type Button}" x:Key="MetroButton"> 
    <Setter Property="MinHeight" Value="25" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="FontSize" Value="10" /> 
    <Setter Property="FontFamily" Value="{DynamicResource DefaultFont}" /> 
    <Setter Property="Background" Value="{DynamicResource GrayBrush7}" /> 
    <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}" /> 
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" /> 
    <Setter Property="Padding" Value="5,6" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PressedBorder"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"> 
             <SplineDoubleKeyFrame KeyTime="0" Value="0.7" /> 
            </DoubleAnimationUsingKeyFrames> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="0.3" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ActiveContent"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1" /> 
            </DoubleAnimationUsingKeyFrames> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ActiveContent"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="Background" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Background="{TemplateBinding Background}" /> 
        <Rectangle x:Name="DisabledVisualElement" 
           Fill="{DynamicResource ControlsDisabledBrush}" 
           IsHitTestVisible="false" 
           Opacity="0" 
           RadiusY="3" 
           RadiusX="3" /> 
        <Border x:Name="MouseOverBorder" 
          Background="{DynamicResource GrayBrush8}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Opacity="0" /> 
        <Border x:Name="ActiveContent" 
          Background="LightGray" 
          Opacity="0" 
          /> 
        <Border x:Name="PressedBorder" 
          Background="{DynamicResource GrayBrush5}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Opacity="0" /> 
        <Rectangle x:Name="FocusRectangle" 
           Stroke="{DynamicResource TextBoxMouseOverInnerBorderBrush}" 
           RadiusY="4" 
           RadiusX="4" 
           Margin="-1" 
           Opacity="0" /> 
        <Rectangle x:Name="FocusInnerRectangle" 
           StrokeThickness="{TemplateBinding BorderThickness}" 
           Stroke="{DynamicResource TextBoxMouseOverBorderBrush}" 
           RadiusX="3" 
           RadiusY="3" 
           Opacity="0" /> 
        <ContentPresenter x:Name="contentPresenter" 
             RecognizesAccessKey="True" 
             ContentTemplate="{TemplateBinding ContentTemplate}" 
             Content="{TemplateBinding Content, Converter={StaticResource ToUpperConverter}}" 
             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
             Margin="{TemplateBinding Padding}" 
             VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

現在我想改變MouseOverForeground.ColorLabel這是Button內。現在樣式設置僅更改ButtonBackgroundMouseOver

回答

-1

試試這個。

<Button Height="36" Margin="286,274,0,0" Width="74" MouseEnter="Button_MouseEnter_1"> 
     <Label x:Name="mylbl" Content="Test" MouseEnter="mylbl_MouseEnter"/> 
</Button> 

後面的代碼:

private void mylbl_MouseEnter(object sender, MouseEventArgs e) 
{ 
     mylbl.Background = new SolidColorBrush(Colors.Red); 
} 
+1

我想我錯過了「沒有C#」的部分。 – Dilshod 2013-03-19 12:01:48

0

將一個子風格爲您的按鈕樣式的Style.Resources內標籤,這是一個砍下的例子,但應該解釋什麼,我想說的。

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style TargetType="Button"> 
      <Style.Resources> 
       <Style TargetType="Label"> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Foreground" Value="Green"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Style.Resources> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="Blue"/> 
       </Trigger> 
      </Style.Triggers> 
       <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid Background="Gray"> 
          <Grid.RowDefinitions> 
           <RowDefinition /> 
           <RowDefinition/> 
          </Grid.RowDefinitions> 

          <!-- This represents your image/--> 
          <Grid Grid.Row="0" Background="{TemplateBinding Background}"/> 
          <Label FontSize="20" Grid.Row="1" Content="{TemplateBinding Content}"/> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Button Margin="172,122,131,79" Foreground="Green"> 
      test 
     </Button> 
    </Grid> 
</Window> 

標籤樣式將不適用於任何按鈕以外,如果你的鼠標在按鍵區的任何部分,它會打開上半部藍色,但只會打開文本綠色鼠標是否在下半部分(由標籤覆蓋)

+0

是的,但這會觸發標籤上的鼠標懸停,而不是按鈕..我需要更改標籤的前景,當按鈕觸發mousover時,或者mb我錯了?因爲我做了類似的事情,並沒有制定出來。 – Pnct 2013-03-20 10:02:14

+0

這樣做,當鼠標懸停在按鈕的任何部分(包括標籤)上時,背景顏色都會改變,當鼠標位於標籤上時,背景保持改變,現在前景也發生改變。 – Andy 2013-03-20 18:41:24

相關問題