Search code examples
wpfbindingdatatrigger

How can I set a datatrigger to select\unselect listbox items?


This issue has been plaguing me for a while, and I just can't get it right. I have multiple listboxes each bound to different lists. In the end, I just want to be able to keep it so only one item is selected at a time across all the lists. Any ideas?

This has got to be very simple, but I have had no luck. I tried the idea of a gloabl\static DependencyObject to match against each ListBoxItem's Tag property, but have trouble getting the datatrigger to fire for each listbox when the value is updated.

For example, if my form has different ListBoxes, each with their own backing list collections and their own DataTemplates, I want to be able to select an item from one list, and have it be the only selected item. Which means I want the others to unselect. I tried binding the ListBoxItem IsSelected Property to a MultiBinding that compares the unique ListBoxItem value to a global value, but had no luck there.

As far as I understand, IsSynchronizedWithCurrentItem works for lists using the same backing collection.


Solution

  • ok, here's a try:

    get the collectionviewsource.View of each of your lists (if you don't use these already, get the default views). In the View you can subscribe to CurrentChanged and in there for each other lists' CollectionViewSource.View.MoveCurrentToPosition(-1);