1

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>

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Mark. I tried doing that but no effect. Is there something obvious that I am missing.
@NainilPatel it is working with your example code for me. Also I made an edit see if it works now. I just posted your example that I modified with the changes. This does work.
1

I would recommend using Style.TargetType, this allows to

  1. Shorten the Setter.Property values
  2. Prevent ambiguity between normal properties and attached properties
  3. Get some IDE validation if the target type actually has that property

TextBlock is not a Control which is why this won't work. Either use TargetType="TextBlock" and change Control.Background to Background or use TextBlock.Background.

(When working with styles also watch out for value precedence, if you set the Background on the TextBlock itself you have a local value which overrides the style completely)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.