7

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

Community
  • 1
  • 1
Thomas Huber
  • 1,282
  • 1
  • 13
  • 23
  • Bind the ListView to the datasource again...Or you use multi select? – st_stefanov Aug 13 '12 at 14:55
  • Does no one have an idea what I am doing wrong here? – Thomas Huber Aug 27 '12 at 12:39
  • 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 Aug 27 '12 at 12:51
  • 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 Aug 27 '12 at 13:21
  • 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 Aug 27 '12 at 14:48

3 Answers3

10

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;
deviantlamb
  • 101
  • 1
  • 4
5

Or you can use this as well:

YourlistView.UnselectAll();
d219
  • 2,707
  • 5
  • 31
  • 36
IntegratedHen
  • 399
  • 4
  • 4
  • I used this for a WPF listview. I added MouseLeftButtonDown event handler and used your code to create a great "click anywhere to deselect" feel. Thanks! – Lars Passic Jun 17 '21 at 05:38
4

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> 
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
EnLaCucha
  • 476
  • 1
  • 8
  • 22