private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Selector s = (Selector)d; object newValue = e.NewValue; // we only want to change the other ones if the change comes from // SelectedItem (otherwise it's already done by the one that was // originally changed (SelectedIndex or SelectedValue) if (!s._selectionChangeIsOnValue && !s._selectionChangeIsOnIndex) { s._selectionChangeIsOnItem = true; try { if (newValue == null) { // we use SetCurrentValue to preserve any potential bindings. s.SetCurrentValue(Selector.SelectedValueProperty, null); s.SetCurrentValue(Selector.SelectedIndexProperty, -1); } else { // we use SetCurrentValue to preserve any potential bindings. s.SetCurrentValue(Selector.SelectedValueProperty, PropertyPathHelper.AccessValueByApplyingPropertyPathIfAny(newValue, s.SelectedValuePath)); s.SetCurrentValue(Selector.SelectedIndexProperty, s.Items.IndexOf(newValue)); s.OnSelectedItemChanged(newValue); if (s.ItemsSource is ICollectionView view) { view.MoveCurrentToPosition(s.Items.IndexOf(newValue)); } } } finally { s._selectionChangeIsOnItem = false; } } //calling the methods to update the Visual Tree/Selection: s.ApplySelectedIndex(s.SelectedIndex); s.ManageSelectedIndex_Changed(s._indexChangeEventArgs); //We do not want to raise the event here when we have a MultiSelector (or when SelectionMode is not Single ?) if (!(s is MultiSelector)) { // Raise the selection changed event List <object> removedItems = new List <object>(); removedItems.Add(e.OldValue); List <object> addedItems = new List <object>(); addedItems.Add(e.NewValue); SelectionChangedEventArgs args = new SelectionChangedEventArgs(removedItems, addedItems); s.OnSelectionChanged(args); } }
private static void OnSelectedIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Selector s = (Selector)d; int newValue = (int)e.NewValue; // we only want to change the other ones if the change comes from // SelectedIndex (otherwise it's already done by the one that was // originally changed (SelectedItem or SelectedValue) if (!s._selectionChangeIsOnValue && !s._selectionChangeIsOnItem) { s._selectionChangeIsOnIndex = true; try { if (newValue == -1) { // Note : we use SetCurrentValue to preserve any potential bindings. s.SetCurrentValue(Selector.SelectedValueProperty, null); // Skip the call to Items.IndexOf() s.SkipCoerceSelectedItemCheck = true; try { // Note: always update the value of SelectedItem last when // synchronizing selection properties, so that all the properties // are up to date when the selection changed event is fired. s.SetCurrentValue(Selector.SelectedItemProperty, null); } finally { s.SkipCoerceSelectedItemCheck = false; } } else { object item = s.Items[newValue]; // Note : we use SetCurrentValue to preserve any potential bindings. s.SetCurrentValue(Selector.SelectedValueProperty, PropertyPathHelper.AccessValueByApplyingPropertyPathIfAny(item, s.SelectedValuePath)); // Skip the call to Items.IndexOf() s.SkipCoerceSelectedItemCheck = true; try { // Note: always update the value of SelectedItem last when // synchronizing selection properties, so that all the properties // are up to date when the selection changed event is fired. s.SetCurrentValue(Selector.SelectedItemProperty, item); } finally { s.SkipCoerceSelectedItemCheck = false; } } } finally { s._selectionChangeIsOnIndex = false; } } s.ApplySelectedIndex(newValue); s.ManageSelectedIndex_Changed(e); }