/// <summary> /// Clears the focus and resets the mouse incrementor object to cancel /// editing and return to mouse drag mode. /// /// https://www.codeproject.com/tips/478376/setting-focus-to-a-control-inside-a-usercontrol-in /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void this_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(delegate() { Keyboard.ClearFocus(); _objMouseIncr = null; })); }
/// <summary> /// Go back to showing <see cref="Cursors.ScrollAll"/> mouse cursor on mouse over /// without keyboard focus. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _PART_TextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (IsMouseDragEnabled == false) { return; } _objMouseIncr = null; (sender as TextBox).Cursor = Cursors.ScrollAll; }
private void _PART_TextBox_LostFocus(object sender, RoutedEventArgs e) { if (IsMouseDragEnabled == true) { _objMouseIncr = null; (sender as TextBox).Cursor = Cursors.ScrollAll; } if (_PART_TextBox != null) { FormatText(_PART_TextBox.Text); } }
private void _PART_TextBox_GotFocus(object sender, RoutedEventArgs e) { var tb = sender as TextBox; _objMouseIncr = null; if (SelectAllTextOnFocus == true) { if (tb != null) { tb.SelectAll(); } } }
private void _PART_TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (IsMouseDragEnabled == false) { return; } if (IsKeyboardFocusWithin == false) { _objMouseIncr = new MouseIncrementor(this.GetPositionFromThis(e), MouseDirections.None); e.Handled = true; } }
/// <summary> /// Is invoked when <see cref="IsMouseDragEnabled"/> dependency property value /// has been changed to update all states accordingly. /// </summary> /// <param name="e"></param> private void OnIsMouseDragEnabledChanged(DependencyPropertyChangedEventArgs e) { _objMouseIncr = null; if (_PART_TextBox != null) { if ((bool)(e.NewValue) == false) { _PART_TextBox.Cursor = Cursors.IBeam; } else { _PART_TextBox.Cursor = Cursors.ScrollAll; } } }
/// <summary> /// Is invoked if/when the user has stopped clicking the mous button /// over the textbox portion of the control. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _PART_TextBox_MouseUp(object sender, MouseButtonEventArgs e) { if (IsMouseDragEnabled == false) { return; } if (_objMouseIncr != null && IsReadOnly == false) { var mouseUpPosition = GetPositionFromThis(e); if (_objMouseIncr.InitialPoint.Equals(mouseUpPosition)) { _PART_TextBox.Focus(); } } _PART_TextBox.ReleaseMouseCapture(); _objMouseIncr = null; }
/// <summary> /// Force <see cref="Cursors.IBeam"/> cursor when keyboard focus is within control. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _PART_TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { _objMouseIncr = null; (sender as TextBox).Cursor = Cursors.IBeam; }
private void _PART_TextBox_MouseMove(object sender, MouseEventArgs e) { if (IsMouseDragEnabled == false) { return; } // nothing to do here if (_objMouseIncr == null) { return; } if (e.LeftButton != MouseButtonState.Pressed) { return; } if (CanIncreaseCommand() == false && CanDecreaseCommand() == false) { // since we can't parse the value, we are out of here, i.e. user put text in our number box _objMouseIncr = null; return; } var pos = GetPositionFromThis(e); double deltaX = (CanMouseDrag == CanIncDecMouseDrag.VerticalOnly ? 0 : _objMouseIncr.Point.X - pos.X); double deltaY = (CanMouseDrag == CanIncDecMouseDrag.HorizontalOnly ? 0 : _objMouseIncr.Point.Y - pos.Y); if (_objMouseIncr.MouseDirection == MouseDirections.None) { // this is our first time here, so we need to record if we are tracking x or y movements if (_objMouseIncr.SetMouseDirection(pos) != MouseDirections.None) { _PART_TextBox.CaptureMouse(); } } if (_objMouseIncr.MouseDirection == MouseDirections.LeftRight) { if (deltaX > 0) { OnDecrement(LargeStepSize); } else { if (deltaX < 0) { OnIncrement(LargeStepSize); } } } else { if (_objMouseIncr.MouseDirection == MouseDirections.UpDown) { if (deltaY > 0) { if (CanIncreaseCommand() == true) { OnIncrease(); } } else { if (deltaY < 0) { if (CanDecreaseCommand() == true) { OnDecrease(); } } } } } _objMouseIncr.Point = GetPositionFromThis(e); }
/// <summary> /// This is called if we are losing the mouse capture without going through /// the MouseUp event - normally this should not be necessary but we'll have /// it as a safety net here. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _PART_TextBox_LostMouseCapture(object sender, MouseEventArgs e) { _objMouseIncr = null; }