Question

I have a ListBox, and there are a few items. How can I change the Background of the ListBoxItem, when mouse hovers it? I tried with this code, but it returns error:

<Window.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Opacity" Value="0.6" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Opacity" Value="1.0" />
                    </Trigger.Setters>
                </Trigger>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Background" To="Orange" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Background" To="White" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
Was it helpful?

Solution

Use a ColorAnimation instead of DoubleAnimation:

<ColorAnimation Duration="0:0:0.3" 
                Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
                To="Orange" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top