/// <inheritdoc />
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            // We have to release the mouse first, in case Validate triggers a Detach of this behavior.
            ReleaseMouseCapture();
            if (dragState == DragState.Starting)
            {
                AssociatedObject.Select(0, AssociatedObject.Text.Length);
                if (!AssociatedObject.IsFocused)
                {
                    Keyboard.Focus(AssociatedObject);
                }
            }
            else if (dragState == DragState.Dragging && AssociatedObject.AllowMouseDrag)
            {
                if (adorner != null)
                {
                    var adornerLayer = AdornerLayer.GetAdornerLayer(AssociatedObject);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Remove(adorner);
                        adorner = null;
                    }
                }
                AssociatedObject.Validate();
            }

            e.Handled            = true;
            Mouse.OverrideCursor = null;
            dragState            = DragState.None;
        }
Beispiel #2
0
        /// <inheritdoc/>
        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            if (!IsContentHostPart(e.OriginalSource))
            {
                return;
            }

            if (AllowMouseDrag && IsReadOnly == false && IsFocused == false)
            {
                dragState = DragState.Starting;

                if (adorner == null)
                {
                    adorner = new DragDirectionAdorner(this, contentHost.ActualWidth);
                    var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Add(adorner);
                    }
                }

                mouseDownPosition = e.GetPosition(this);

                Mouse.OverrideCursor = Cursors.None;
                e.Handled            = true;
            }
        }
        /// <inheritdoc />
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            if (!IsContentHostPart(e.OriginalSource))
            {
                return;
            }

            if (!AssociatedObject.AllowMouseDrag || AssociatedObject.IsReadOnly || AssociatedObject.IsFocused)
            {
                return;
            }

            e.Handled = true;
            CaptureMouse();

            dragState            = DragState.Starting;
            Mouse.OverrideCursor = Cursors.None;
            mouseDownPosition    = e.GetPosition(AssociatedObject);

            if (adorner == null)
            {
                adorner = new DragDirectionAdorner(AssociatedObject, AssociatedObject.contentHost.ActualWidth);
                var adornerLayer = AdornerLayer.GetAdornerLayer(AssociatedObject);
                adornerLayer?.Add(adorner);
            }
        }
Beispiel #4
0
        /// <inheritdoc/>
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);

            if (ReferenceEquals(e.MouseDevice.Captured, this))
            {
                e.MouseDevice.Capture(null);
            }

            if (dragState == DragState.Starting)
            {
                Select(0, Text.Length);
                if (!IsFocused)
                {
                    Keyboard.Focus(this);
                }
            }
            else if (dragState == DragState.Dragging && AllowMouseDrag)
            {
                if (adorner != null)
                {
                    var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Remove(adorner);
                        adorner = null;
                    }
                }
                Validate();
            }

            Mouse.OverrideCursor = null;
            dragState            = DragState.None;
        }
Beispiel #5
0
        /// <inheritdoc/>
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);

            if (ReferenceEquals(e.MouseDevice.Captured, this))
            {
                e.MouseDevice.Capture(null);
            }

            if (dragState == DragState.Starting)
            {
                Select(0, Text.Length);
                if (!IsFocused)
                {
                    Keyboard.Focus(this);
                }
            }
            else if (dragState == DragState.Dragging && AllowMouseDrag)
            {
                if (adorner != null)
                {
                    var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Remove(adorner);
                        adorner = null;
                    }
                }
                Validate();

                var handler = DragCompleted;
                if (handler != null)
                {
                    Point  position = e.GetPosition(this);
                    double dx       = Math.Abs(position.X - mouseDownPosition.X);
                    double dy       = Math.Abs(position.Y - mouseDownPosition.Y);
                    handler(this, new DragCompletedEventArgs(dx, dy, false));
                }
            }

            Mouse.OverrideCursor = null;
            dragState            = DragState.None;
        }
Beispiel #6
0
        private void RootParentIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.NewValue == false)
            {
                // Cancel dragging in progress
                if (dragState == DragState.Dragging)
                {
                    if (adorner != null)
                    {
                        var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                        if (adornerLayer != null)
                        {
                            adornerLayer.Remove(adorner);
                            adorner = null;
                        }
                    }
                    Cancel();

                    var handler = DragCompleted;
                    if (handler != null)
                    {
                        Point  position = Mouse.GetPosition(this);
                        double dx       = Math.Abs(position.X - mouseDownPosition.X);
                        double dy       = Math.Abs(position.Y - mouseDownPosition.Y);
                        handler(this, new DragCompletedEventArgs(dx, dy, true));
                    }

                    Mouse.OverrideCursor = null;
                    dragState            = DragState.None;
                }

                var root = this.FindVisualRoot() as FrameworkElement;
                if (root != null)
                {
                    root.IsKeyboardFocusWithinChanged -= RootParentIsKeyboardFocusWithinChanged;
                }
            }
        }
        private void RootParentIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.NewValue)
            {
                return;
            }

            // Cancel dragging in progress
            if (dragState == DragState.Dragging)
            {
                if (adorner != null)
                {
                    var adornerLayer = AdornerLayer.GetAdornerLayer(AssociatedObject);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Remove(adorner);
                        adorner = null;
                    }
                }
                AssociatedObject.Cancel();
            }
            Cancel();
        }
Beispiel #8
0
        /// <inheritdoc/>
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);

            if (ReferenceEquals(e.MouseDevice.Captured, this))
                e.MouseDevice.Capture(null);

            if (dragState == DragState.Starting)
            {
                Select(0, Text.Length);
                if (!IsFocused)
                {
                    Keyboard.Focus(this);
                }
            }
            else if (dragState == DragState.Dragging && AllowMouseDrag)
            {                
                if (adorner != null)
                {
                    var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                    if (adornerLayer != null)
                    {
                        adornerLayer.Remove(adorner);
                        adorner = null;
                    }
                }
                Validate();

                var handler = DragCompleted;
                if (handler != null)
                {
                    Point position = e.GetPosition(this);
                    double dx = Math.Abs(position.X - mouseDownPosition.X);
                    double dy = Math.Abs(position.Y - mouseDownPosition.Y);
                    handler(this, new DragCompletedEventArgs(dx, dy, false));
                }
            }

            Mouse.OverrideCursor = null;
            dragState = DragState.None;
        }
Beispiel #9
0
        /// <inheritdoc/>
        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            if (!IsContentHostPart(e.OriginalSource))
                return;

            if (AllowMouseDrag && IsReadOnly == false && IsFocused == false)
            {
                dragState = DragState.Starting;

                if (adorner == null)
                {
                    adorner = new DragDirectionAdorner(this, contentHost.ActualWidth);
                    var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                    if (adornerLayer != null)
                        adornerLayer.Add(adorner);
                }

                mouseDownPosition = e.GetPosition(this);

                Mouse.OverrideCursor = Cursors.None;
                e.Handled = true;
            }
        }
Beispiel #10
0
        private void RootParentIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.NewValue == false)
            {
                // Cancel dragging in progress
                if (dragState == DragState.Dragging)
                {
                    if (adorner != null)
                    {
                        var adornerLayer = AdornerLayer.GetAdornerLayer(this);
                        if (adornerLayer != null)
                        {
                            adornerLayer.Remove(adorner);
                            adorner = null;
                        }
                    }
                    Cancel();

                    var handler = DragCompleted;
                    if (handler != null)
                    {
                        Point position = Mouse.GetPosition(this);
                        double dx = Math.Abs(position.X - mouseDownPosition.X);
                        double dy = Math.Abs(position.Y - mouseDownPosition.Y);
                        handler(this, new DragCompletedEventArgs(dx, dy, true));
                    }

                    Mouse.OverrideCursor = null;
                    dragState = DragState.None;

                }

                var root = this.FindVisualRoot() as FrameworkElement;
                if (root != null)
                    root.IsKeyboardFocusWithinChanged -= RootParentIsKeyboardFocusWithinChanged;
            }
        }