MouseUpCommand只是偶尔触发

时间:2016-03-16 17:12:51

标签: wpf mvvm mouseevent

我创建了一个attched属性来将MouseUpCommand和MouseDownCommand绑定到我的ViewModel。当MouseDownCommand按原样触发时,MouseUpCommand只会偶尔触发。有谁知道我做错了什么?

这是我的XAML:

    <ItemsControl ItemsSource="{Binding Page.Collection}" Grid.Row="1" Grid.Column="1" 
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas  IsItemsHost="True" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Background="Blue"
                    local:MouseBehaviour.MouseUpCommand="{Binding ViewModel.MouseUpCommand}"
                    local:MouseBehaviour.MouseDownCommand="{Binding ViewModel.MouseDownCommand}"
                    local:MouseBehaviour.MouseMoveCommand="{Binding ViewModel.MouseMoveCommand}">
                </Canvas>

这是MouseBehavior:

public class MouseBehaviour
{
    #region MouseUp

    public static readonly DependencyProperty MouseUpCommandProperty =
        DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseUpCommandChanged)));

    private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;

        element.PreviewMouseUp += element_MouseUp;
    }

    static void element_MouseUp(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;

        ICommand command = GetMouseUpCommand(element);

        command.Execute(e);
    }



    public static void SetMouseUpCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseUpCommandProperty, value);
    }

    public static ICommand GetMouseUpCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseUpCommandProperty);
    }

    #endregion

    #region MouseDown

    public static readonly DependencyProperty MouseDownCommandProperty =
        DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseDownCommandChanged)));

    private static void MouseDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;

        element.PreviewMouseDown += element_MouseDown;
    }

    static void element_MouseDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;

        ICommand command = GetMouseDownCommand(element);

        command.Execute(e);
    }

    public static void SetMouseDownCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseDownCommandProperty, value);
    }

    public static ICommand GetMouseDownCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseDownCommandProperty);
    }

    #endregion
}

我尝试使用普通鼠标命令以及预览鼠标命令。没有区别。

1 个答案:

答案 0 :(得分:0)

如果我打破了MVVM模式,只需将事件添加到程序背后的代码中就可以完美地运行:

    private void ItemsControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
        mainViewModel.ViewModel.MouseUp(e);
    }

    private void ItemsControl_MouseMove(object sender, MouseEventArgs e)
    {
        mainViewModel.ViewModel.MouseMove(e);
    }

    private void ItemsControl_MouseDown(object sender, MouseButtonEventArgs e)
    {
        mainViewModel.ViewModel.MouseDown(e);
    }
相关问题