2013-02-27 138 views
0

我正在学习WPF。我试图通过使用Style.TriggerTextBlock应用BackgroundForeground。从我定义的Trigger中,我可以注意到MouseOver上的Foreground被更改,但不是Background。你能帮忙吗?下面是我的XAML:WPF TextBlock BackGround未使用风格设置

<Window x:Class="WpfApplication1.ApplyingStyle" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="ApplyingStyle" 
     Height="300" 
     Width="300"> 
    <Window.Resources> 
    <Style x:Key="myStyle"> 
     <Setter Property="Control.Background" 
       Value="Red" /> 
     <Setter Property="Control.FontFamily" 
       Value="Times New Roman" /> 
     <Setter Property="Control.FontSize" 
       Value="25" /> 
     <Style.Triggers> 
     <Trigger Property="Control.IsMouseOver" 
       Value="True"> 
      <Setter Property="Control.Foreground" 
        Value="HotPink" /> 
      <Setter Property="Control.Background" 
        Value="Blue" /> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Window.Resources> 
    <Grid ShowGridLines="True"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <TextBlock Name="myTextBlock" 
       Grid.Row="1" 
       Grid.Column="0" 
       Style="{StaticResource myStyle}">Hello</TextBlock> 
    </Grid> 
</Window> 

回答

0

尝试使用您要的样式应用到你的风格宣言中的控制类型,而不是使用Control.Background使用TextBlock.Background或只设置TargetTypeTextBlock。当我将你的风格设置为这样的东西时,它似乎工作。

<Window x:Class="WpfApplication1.ApplyingStyle" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="ApplyingStyle" Height="300" Width="300"> 
    <Window.Resources> 
     <Style x:Key="myStyle" TargetType="TextBlock"> 
      <Setter Property="Background" Value="Red" /> 
      <Setter Property="FontFamily" Value="Times New Roman" /> 
      <Setter Property="FontSize" Value="25" /> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True" > 
        <Setter Property="Background" Value="Blue" /> 
        <Setter Property="Foreground" Value="HotPink" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <Grid ShowGridLines="True" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition/> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <TextBlock Name="myTextBlock" 
        Grid.Row="1" 
        Grid.Column="0" 
        Style="{StaticResource myStyle}">Hello</TextBlock> 
    </Grid> 
</Window> 
+0

感谢马克。我试过这样做,但没有效果。有什么明显的,我失踪了。 – 2013-02-27 03:08:25

+0

@NainilPatel它正在处理您的示例代码。另外我做了一个编辑,看它现在是否有效。我刚刚发布了你的示例,我修改了这些修改。这确实有用。 – 2013-02-27 03:08:46

+0

是的,这确实有效。非常感谢Mark。 – 2013-02-27 03:18:36

1

我会建议使用Style.TargetType,这允许

  1. 缩短Setter.Property
  2. 正常属性和附加属性
  3. 得到一些IDE验证如果目标类型实际上具有之间
  4. 避免含混该财产

TextBlock不是 a Control这就是为什么这不起作用。使用TargetType="TextBlock"并将Control.Background更改为Background或使用TextBlock.Background

(当与风格的工作还注意value precedence,如果你对TextBlock设置Background本身你有局部值它完全覆盖风格)

+0

感谢您的回复H.B. – 2013-02-27 03:19:13