Пример #1
0
        static void SafeExecute(this ICommand dragCommand, ClickDragInfo dragInfo, object parameter)
        {
            var args = new MouseDragCommandArgs {
                DragInfo = dragInfo, Parameter = parameter
            };

            if (dragCommand != null && dragCommand.CanExecute(args))
            {
                dragCommand.Execute(args);
            }
        }
        static void OnMouseButtonDown( object sender, MouseButtonEventArgs e )
        {
            var element = (FrameworkElement)sender;

            MouseState mouseState = GetMouseState( e, element );

            // Trigger down commands.
            ICommand leftDownCommand = GetLeftMouseDownCommand( element );
            if ( e.ChangedButton == MouseButton.Left )
            {
                leftDownCommand.SafeExecute( mouseState );
            }
            ICommand rightDownCommand = GetRightMouseDownCommand( element );
            if ( e.ChangedButton == MouseButton.Right )
            {
                rightDownCommand.SafeExecute( mouseState );
            }

            // Track click commands.
            ICommand leftClickCommand = e.ChangedButton == MouseButton.Left
                ? GetLeftClickCommand( element )
                : GetRightClickCommand( element );
            Dictionary<object, ClickDragInfo> clickInfo = e.ChangedButton == MouseButton.Left
                ? LeftClickInfo
                : RightClickInfo;
            if ( leftClickCommand != null && e.ChangedButton == MouseButton.Left )
            {
                var info = new ClickDragInfo
                {
                    Mouse = mouseState,
                    State = ClickDragState.Start,
                    StartPosition = mouseState.Position
                };

                if ( !clickInfo.ContainsKey( sender ) )
                {
                    clickInfo.Add( sender, info );
                }
            }
            _previousPosition = mouseState.Position.Relative;
            _distanceDragged = 0;

            // Trigger click drag commands.
            ICommand clickDragCommand = e.ChangedButton == MouseButton.Left
                ? GetLeftClickDragCommand( element )
                : GetRightClickDragCommand( element );
            Dictionary<object, ClickDragInfo> dragInfo = e.ChangedButton == MouseButton.Left
                ? LeftClickDragInfo
                : RightClickDragInfo;
            if ( clickDragCommand != null )
            {
                if ( GetDragCapturesMouse( element ) )
                {
                    // TODO: What to do if capturing the mouse fails?
                    element.CaptureMouse();
                }

                var info = new ClickDragInfo
                {
                    Mouse = mouseState,
                    State = ClickDragState.Start,
                    StartPosition = mouseState.Position
                };
                clickDragCommand.SafeExecute( info );

                if ( !dragInfo.ContainsKey( sender ) )
                {
                    dragInfo.Add( sender, info );
                }
            }

            e.Handled = true;
        }
		static void SafeExecute( this ICommand dragCommand, ClickDragInfo dragInfo, object parameter )
		{
			var args = new MouseDragCommandArgs { DragInfo = dragInfo, Parameter = parameter };
			if ( dragCommand != null && dragCommand.CanExecute( args ) )
			{
				dragCommand.Execute( args );
			}
		}
Пример #4
0
        static void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            var        element      = (FrameworkElement)sender;
            MouseState mouseState   = GetMouseState(e, element);
            bool       isLeftButton = e.ChangedButton == MouseButton.Left;

            // Trigger down commands.
            ICommand leftDownCommand = GetLeftMouseDownCommand(element);

            if (isLeftButton)
            {
                leftDownCommand.SafeExecute(mouseState, GetLeftMouseDownCommandParameter(element));
            }
            ICommand rightDownCommand = GetRightMouseDownCommand(element);

            if (e.ChangedButton == MouseButton.Right)
            {
                rightDownCommand.SafeExecute(mouseState, GetRightMouseDownCommandParameter(element));
            }

            // Track click commands.
            ICommand clickCommand = isLeftButton ? GetLeftClickCommand(element) : GetRightClickCommand(element);
            Dictionary <object, ClickDragInfo> clickInfo = isLeftButton ? LeftClickInfo : RightClickInfo;

            if (clickCommand != null)
            {
                var info = new ClickDragInfo
                {
                    Sender        = element,
                    Mouse         = mouseState,
                    State         = ClickDragState.Start,
                    StartPosition = mouseState.Position
                };

                if (!clickInfo.ContainsKey(sender))
                {
                    clickInfo.Add(sender, info);
                }
            }
            MoveState moveState;

            MoveStates.TryGetValue(element, out moveState);
            moveState.PreviousPosition = mouseState.Position.Relative;
            moveState.DistanceDragged  = 0;
            MoveStates[element]        = moveState;

            // Trigger click drag commands.
            ICommand clickDragCommand   = isLeftButton ? GetLeftClickDragCommand(element) : GetRightClickDragCommand(element);
            object   clickDragParameter = isLeftButton ? GetLeftClickDragCommandParameter(element) : GetRightClickDragCommandParameter(element);
            Dictionary <object, ClickDragInfo> dragInfo = isLeftButton ? LeftClickDragInfo : RightClickDragInfo;

            if (clickDragCommand != null)
            {
                if (GetDragCapturesMouse(element))
                {
                    // TODO: What to do if capturing the mouse fails?
                    element.CaptureMouse();
                }

                var info = new ClickDragInfo
                {
                    Sender        = element,
                    Mouse         = mouseState,
                    State         = ClickDragState.Start,
                    StartPosition = mouseState.Position
                };
                clickDragCommand.SafeExecute(info, clickDragParameter);

                if (!dragInfo.ContainsKey(sender))
                {
                    dragInfo.Add(sender, info);
                }
            }

            e.Handled = true;
        }
		static void OnMouseButtonDown( object sender, MouseButtonEventArgs e )
		{
			var element = (FrameworkElement)sender;
			MouseState mouseState = GetMouseState( e, element );
			bool isLeftButton = e.ChangedButton == MouseButton.Left;

			// Trigger down commands.
			ICommand leftDownCommand = GetLeftMouseDownCommand( element );
			if ( isLeftButton )
			{
				leftDownCommand.SafeExecute( mouseState, GetLeftMouseDownCommandParameter( element ) );
			}
			ICommand rightDownCommand = GetRightMouseDownCommand( element );
			if ( e.ChangedButton == MouseButton.Right )
			{
				rightDownCommand.SafeExecute( mouseState, GetRightMouseDownCommandParameter( element ) );
			}

			// Track click commands.
			ICommand clickCommand = isLeftButton ? GetLeftClickCommand( element ) : GetRightClickCommand( element );
			Dictionary<object, ClickDragInfo> clickInfo = isLeftButton ? LeftClickInfo : RightClickInfo;
			if ( clickCommand != null )
			{
				var info = new ClickDragInfo
				{
					Sender = element,
					Mouse = mouseState,
					State = ClickDragState.Start,
					StartPosition = mouseState.Position
				};

				if ( !clickInfo.ContainsKey( sender ) )
				{
					clickInfo.Add( sender, info );
				}
			}
			MoveState moveState;
			MoveStates.TryGetValue( element, out moveState );
			moveState.PreviousPosition = mouseState.Position.Relative;
			moveState.DistanceDragged = 0;
			MoveStates[ element ] = moveState;

			// Trigger click drag commands.
			ICommand clickDragCommand = isLeftButton ? GetLeftClickDragCommand( element ) : GetRightClickDragCommand( element );
			object clickDragParameter = isLeftButton ? GetLeftClickDragCommandParameter( element ) : GetRightClickDragCommandParameter( element );
			Dictionary<object, ClickDragInfo> dragInfo = isLeftButton ? LeftClickDragInfo : RightClickDragInfo;
			if ( clickDragCommand != null )
			{
				if ( GetDragCapturesMouse( element ) )
				{
					// TODO: What to do if capturing the mouse fails?
					element.CaptureMouse();
				}

				var info = new ClickDragInfo
				{
					Sender = element,
					Mouse = mouseState,
					State = ClickDragState.Start,
					StartPosition = mouseState.Position
				};
				clickDragCommand.SafeExecute( info, clickDragParameter );

				if ( !dragInfo.ContainsKey( sender ) )
				{
					dragInfo.Add( sender, info );
				}
			}

			e.Handled = true;
		}