How do I prevent a ListViewItem from being selected when I click on an item and press (Down/Up) Arrow? I don't want to disable it, I just want it to not be selectable on Shift+Arrow. Some of my listviewitems need to be selectable and some others not.
Sample:
[ListViewItem 1]
[ListViewItem 2]
[ListViewItem 3]
User clicks on ListviewItem1, it gets selected.
User then presses SHIFT+DOWN ARROW,=> ListViewItem 2 is not selected.
At this point either ListViewItem3 is selected or it might take another SHIFT+DOWN ARROW to select ListViewItem3 (it doesn't matter as long as it gets selected sometime).
(In my project the actual ListViewItem 2 is actually NOT a regular item on 1 row, it is a vertical item that spans several rows in just one column. My ListView source has a compositecollection of very different items; I am trying to select what appears to be 'regular row items' only)
You can accomplish this by using a
Propertyof your class such asIsSelected. Here is some sample code for what thatPropertywould look like.In this example it is assumed that "isSelectable" is a value that is set in the constructor when your class is being initialized. If you don't already have
INotifyPropertyChangedimplemented, it needs to be... and thePropertyChangedevent needs to be declared.In addition, you will also want a
Stylethat binds the selection ofListViewitems to this property. Here is an example of aStylethat could accomplish this.You can take this one step further, and also bind to the
FocusableProperty. This will make it so that when you press SHIFT+DOWN it will skip any items that you cannot select and select the next item that is selectable. I would accomplish this in a manner similar to the following.You would also need to create a binding for this in your
Styleby creating an additionalSetter. This could be done the following way, inside the sameStyleas before.Try it both with and without that last
Setterand see which implementation suits your needs best.--------- UPDATE ---------
If you would like to only select items on mouse click, you could do so by subscribing to the
ListViewPreviewMouseLeftButtonDownevent. Inside of the event handler, you would need to first get the clicked item, and call a function on your class which will override the selection. Here is an example of how that is done.This function would exist in your UI code-behind and must be subscribed to by your
ListView:(Note: replace "MyClass" with your class type).
This function would exist in your class file: