-1

I have this ListViewItem trigger:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter Property="Foreground" Value="Gray"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="Height" Value="50"/>
</MultiDataTrigger>

When I am selecting my ListViewItem, this item becomes larger so I can show another elements.

Now I want to implement a behavior that after each click on a ListViewItem this item will change from selected to not selected, so after each click my ListViewItem changes its height to 50 and after another click back to 22 (the default size).

I subscribed to an PreviewMouseLeftButtonDown event:

private void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}

My question is: what do I need to write into this event handler?

Clemens
  • 123,504
  • 12
  • 155
  • 268
falukky
  • 1,099
  • 2
  • 14
  • 34

1 Answers1

1

You could handle the PreviewMouseLeftButtonDown event for the ListViewItem container:

<ListView x:Name="listView">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
            <Style.Triggers>
                <MultiDataTrigger>
                    ...
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

...something like this:

private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ListViewItem lvi = (ListViewItem)sender;
    if (lvi.IsSelected)
    {
        listView.SelectedItems.Remove(lvi.DataContext);
        e.Handled = true;
    }
}

I put this EventSetter after my Style (this style is in another file) and got this: 'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file ...

If you define the ItemContainerStyle in a ResourceDictionary, you need to add a code-behind file to the ResourceDictionary. This is an easy thing to do:

Is it possible to set code behind a resource dictionary in WPF for event handling?

The other option would be to define the Style with the EventSetter inline in your view and base it on the Style with the MultiDataTrigger that you have defined in the ResourceDictionary:

<ListView x:Name="listView">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem" BasedOn="{StaticResource YourOtherStyleInTheResourceDictionary}">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I put this EventSetter after my Style (this style is in another file) and got this: 'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the PreviewMouseLeftButtonDown event, or add a x:Class attribute to the root element. – falukky Jul 18 '18 at 09:20
  • If you define the ItemContainerStyle in a ResourceDictionary you need to add a code-behind file to the ResourceDictionary: https://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handlin. Or you could just another ItemContainerStyle in your view and base this one on the one defined in the ResourceDictionary. – mm8 Jul 18 '18 at 09:22
  • I sorry but i did not understand what to do, i have this ListViewItem.xaml file and inside App.xaml the declaration for this style file, so whay i should add ? – falukky Jul 18 '18 at 09:25
  • In your question, you said that you "subscribed to an PreviewMouseLeftButtonDown event". Where did you define this event handler? – mm8 Jul 18 '18 at 09:26
  • In my ListView XAML – falukky Jul 18 '18 at 09:27
  • And what is that? A window? A UserControl? – mm8 Jul 18 '18 at 09:28
  • simple ListView that i added in the main XAML – falukky Jul 18 '18 at 09:29
  • See my edit. Define the event handler in the code-behind of your "main XAML" and base the style in your "main XAML" on the one in your ResourceDictionary, i.e. replace "YourOtherStyleInTheResourceDictionary" with the x:Key of the Style in your ResourceDictionary. – mm8 Jul 18 '18 at 09:32