6
votes

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

3
Bind the ListView to the datasource again...Or you use multi select? - st_stefanov
Does no one have an idea what I am doing wrong here? - Thomas Huber
Are you changing IsSelected on the ui element or on your bound data context? If it is your DataContext, does it implement INotifyPropertyChanged? And does the IsSelected raises the change? - dowhilefor
Also are there any binding exceptions in output window? If not, try to use extended info about binding with setting Value="{Binding Path=IsSelected, diagnostics:PresentationTraceSources.TraceLevel=High}" - dvvrd
Hi. Yes I am changing the value of my bound data context. And the INotifyPropertyChanged is raised. (IsSelected = true) successfully selects an element. Using TraceLevel=High I am able to confirm that it seems like the binding works. IsSelect = true in code results in TransferValue - using final value 'True' and IsSelect = false in code results in TransferValue - using final value 'False'. However within the view the item is still selected. - Thomas Huber

3 Answers

8
votes

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;
4
votes

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> 
2
votes

Or you can use this as well:

YourlistView.UnselectAll();