private void OnselectionModelChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "SelectedIndices") { bool?oldValue = IsSelected; var indexPath = GetIndexPath(); bool?newValue = IsRealized(indexPath) ? SelectionModel.IsSelectedAt(indexPath) : false; if (oldValue != newValue) { IsSelected = newValue; bool oldValueAsBool = oldValue.HasValue && oldValue.Value; bool newValueAsBool = newValue.HasValue && newValue.Value; if (oldValueAsBool != newValueAsBool) { // AutomationEvents.PropertyChanged is used as a value that means dont raise anything AutomationEvents eventToRaise = oldValueAsBool ? (SelectionModel.SingleSelect ? AutomationEvents.PropertyChanged : AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection) : (SelectionModel.SingleSelect ? AutomationEvents.SelectionItemPatternOnElementSelected : AutomationEvents.SelectionItemPatternOnElementAddedToSelection); if (eventToRaise != AutomationEvents.PropertyChanged && AutomationPeer.ListenerExists(eventToRaise)) { var peer = FrameworkElementAutomationPeer.CreatePeerForElement(this); peer.RaiseAutomationEvent(eventToRaise); } } } } }
protected override void OnKeyUp(KeyEventArgs e) { var indexPath = GetIndexPath(); Debug.WriteLine("OnKeyUp:" + indexPath.ToString()); if (SelectionModel != null) { if (e.Key == Key.Escape) { SelectionModel.ClearSelection(); } else if (e.Key == Key.Space) { SelectionModel.SelectAt(indexPath); } else if (!SelectionModel.SingleSelect) { var isShiftPressed = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift); var isCtrlPressed = Keyboard.Modifiers.HasFlag(ModifierKeys.Control); if (e.Key == Key.A && isCtrlPressed) { SelectionModel.SelectAll(); } else if (isCtrlPressed && e.Key == Key.Space) { if (SelectionModel.IsSelectedAt(indexPath).Value) { SelectionModel.DeselectAt(indexPath); } else { SelectionModel.SelectAt(indexPath); } } else if (isShiftPressed) { SelectionModel.SelectRangeFromAnchorTo(GetIndexPath()); } } } base.OnKeyUp(e); }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (!e.Handled) { var indexPath = GetIndexPath(); Debug.WriteLine("OnPointerPressed:" + indexPath.ToString()); if (SelectionModel != null) { if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) && !SelectionModel.SingleSelect) { if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) { SelectionModel.DeselectRangeFromAnchorTo(GetIndexPath()); } else { SelectionModel.SelectRangeFromAnchorTo(GetIndexPath()); } } else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) { var path = GetIndexPath(); if (SelectionModel.IsSelectedAt(path).Value) { SelectionModel.SelectAt(path); } else { SelectionModel.SelectAt(path); } } else { SelectionModel.SelectAt(GetIndexPath()); this.Focus(); } } e.Handled = true; base.OnMouseLeftButtonDown(e); } }