Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView programmatically deselect item

I have used the following approach to bind IsSelected of my items to a Property: WPF ListView Programmatically Select Item

<ListView.ItemContainerStyle>
    <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</ListView.ItemContainerStyle>

Now I am able to select my items in code behind by simple setting the IsSelected property to true. However I am not able to deselect items by setting the IsSelected property of my items to false.

Setting the items property IsSelected to true will trigger the ListViewSelectionChanged event. However setting the property IsSelected of an already selected item to false does not trigger the event. The property will be changed to false but the item remains selected within the ListView. I have also tried using Mode=TwoWay without any success.

I would appreciate any sort of help!

Thank you very much in advance,

Thomas

like image 765
Thomas Huber Avatar asked Aug 13 '12 14:08

Thomas Huber


2 Answers

For OP or others looking to "programmatically" deselect a ListView. If your ListView rigged up as Single, Extended or Multiple you can always just:

YourlistView.Selecteditem = null;
like image 109
deviantlamb Avatar answered Oct 15 '22 01:10

deviantlamb


Looks like you are just missing the TargetType for the style. Add the target type of ListViewItem as per Kent's original code below.

<ListView.ItemContainerStyle>         
    <Style TargetType="ListViewItem">             
        <Setter Property="IsSelected" Value="{Binding IsGroovy}"/>               
    </Style>   
</ListView.ItemContainerStyle> 
like image 22
EnLaCucha Avatar answered Oct 15 '22 01:10

EnLaCucha