2

I have a mouseover-selecting listview by using trigger:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="IsSelected" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

However when mouse is moved out, the trigger sets IsSelected back to false, which is not desired here. Is there a way to make a trigger only set-upon-enter but not reset-upon-exit?

NS.X.
  • 2,072
  • 5
  • 28
  • 55

1 Answers1

3

This is what EventTriggers are for:

Unlike Trigger, EventTrigger has no concept of termination of state, so the action will not be undone once the condition that raised the event is no longer true.

In your case for example:

<EventTrigger RoutedEvent="MouseEnter">
    <BeginStoryboard>
        <Storyboard>
            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
            </BooleanAnimationUsingKeyFrames>
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>
LPL
  • 16,827
  • 6
  • 51
  • 95
  • Thanks. Will it be a problem that the storyboard never ends? – NS.X. May 21 '12 at 23:38
  • I think it ends. Take a look at [What Happens After an Animation Ends?](http://msdn.microsoft.com/en-us/library/ms752312#fillbehaviorsection) – LPL May 21 '12 at 23:46
  • Sounds good. As a side note, there is some issue I don't really understand and haven't run into mentioned in the other thread http://stackoverflow.com/questions/942548/setting-a-property-with-an-eventtrigger – NS.X. May 22 '12 at 02:53