Why this property does not implement this, I do not know, but I have a suggestion below.
Your code above will work, however this is not what the IsSynchronizedWithCurrentItem property does. Any ItemsControl is associated with an ICollectionView of the ItemsSource property. To get this ICollectionView, we can call CollectionViewSource.GetDefaultCollectionView (object o). Depending on the type of object o, you get another specific implementation of ICollectionView inteface. CollectionView and ListCollectionView are two specific classes that come to mind.
The ICollectionView interface contains a member called CurrentItem. What IsSynchronizedWithCurrentItem does: whenever an item is clicked on an ItemsControl, it sets CurrentItem to view the collection. ICollectionView also has two events: CurrentItemChanging and CurrentItemChanged. When the IsSynchronizedWithCurrentItem property is set, ItemControl will update SelectedItem based on what ICollectionView CurrentItem is. Has the meaning?
In master / detail WPF scripting scripts, we simply bind to ICollectionViews and their CurrentItem (the syntax of CurrentItem is something like {Binding Items / Name}, where Name is the Name property in the CurrentItem collection.
So, although your code works for your purposes, it does not do what this property does. To do what the property does, you need to do the following:
- When an item is selected, you need to find out which collection it belongs to. How do we do this? I believe that is why TreeView does not implement it. The selected item is displayed in the TreeViewItem. DataContext is the object itself, but what is a parent collection? I think in order to get it, you can either cache it in some hashmap (stupid, but work), or you can go to the logical tree and get the parent TreeViewItem, which turns out to be the ItemsControl. The ItemsSource property will be your collection.
- Get the ICollectionView for this collection.
- You need to use this ICollectionView in the CollectionView (ICollectionView does not implement the CurrentItem customization tool)
- Call SetCurrent (.., ..) on an instance of CollectionView
Now everything related to this ICollectionView CurrentItem will be updated.
It has become more than I expected. Let me know if any clarification is needed.
Szymon rozga
source share