Search code examples
wpftextblock

WPF TextBlock BackGround not being set using style


I am learing WPF. I was trying to apply Background and Foreground for a TextBlock using Style.Trigger. From my defined Trigger, I can notice the Foreground being changed on MouseOver, but not Background. Can you please help. Below is my XAML:

<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>

Solution

  • Try using the control Type that you are trying to apply the Style to in your Style Declaration, instead of using Control.Background use TextBlock.Background or just set the TargetType to TextBlock. It appears to work when I set your style to something like this.

    <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>