Adrian
Adrian

Reputation: 20058

IsMouseOver event defined in a style is not triggered

This is defined in App.xaml inside <Application.Resources> :

<Style x:Key="borderStyle" TargetType="Border">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

And I am using it here inside Window1.xaml :

<ListBox Name="listView1" ItemsSource="{Binding}" Background="Black" >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"  Orientation="Horizontal" ItemWidth="150" ItemHeight="150" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Border BorderThickness="5" BorderBrush="DarkGray" Style="{StaticResource borderStyle}">
                            <Image  Width="120"  Height="120" Stretch="Fill"  Source="{Binding Image}" />
                         </Border>
                        <TextBlock FontFamily="Verdana"  Foreground="Orange"  Text="{Binding Title}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

But it doesn't work, when I mouse over it, the border doesn't change the color.

What am I doing wrong ?

Thanks.

Upvotes: 0

Views: 128

Answers (1)

DanielB
DanielB

Reputation: 20210

You are overriding the trigger if you define the property directly within the border.

Remove BorderBrush="DarkGray"from this line

<Border BorderThickness="5" BorderBrush="DarkGray" Style="{StaticResource borderStyle}">

so it looks like this

<Border BorderThickness="5" Style="{StaticResource borderStyle}">

and add a setter to your style

<Style x:Key="borderStyle" TargetType="Border">
    <Setter Property="BorderBrush" Value="DarkGray" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

As a thumb rule: all properties which should be modified by triggers have to be defined as setters within the style.

Upvotes: 3

Related Questions