将MouseButtonEventArgs后面的代码转换为MVVM

时间:2017-11-23 04:55:40

标签: c# wpf canvas mvvm

我还是MVVM的新手,但是我试图学习它。

我需要将绘制的矩形调整到我的画布中。但我想用MVVM方式做到这一点。

我发现这篇文章及其工作,它满足了我的需求,但它不是MVVM

https://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/

我正在尝试将此代码转换为MVVM

// Handler for element selection on the canvas providing resizing adorner
        void myCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Remove selection on clicking anywhere the window
            if (selected)
            {
                selected = false;
                if (selectedElement != null)
                {
                    // Remove the adorner from the selected element
                    aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);                    
                    selectedElement = null;
                }
            }

            // If any element except canvas is clicked, 
            // assign the selected element and add the adorner
            if (e.Source != myCanvas)
            {
                _isDown = true;
                _startPoint = e.GetPosition(myCanvas);

                selectedElement = e.Source as UIElement;

                _originalLeft = Canvas.GetLeft(selectedElement);
                _originalTop = Canvas.GetTop(selectedElement);

                aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
                aLayer.Add(new ResizingAdorner(selectedElement));
                selected = true;
                e.Handled = true;
            }
        }

我正在使用Devexpress MVVM

这是我的xaml代码

 <Canvas Name="myCanvas" Background="Gray">
        <dxmvvm:Interaction.Behaviors>
            <dxmvvm:EventToCommand Command="{Binding OnSelectedCommand , Source={x:Static vm:Vm.instance}}" 
                                       EventName="MouseDown" CommandParameter="{Binding ElementName=myCanvas}"
                                       PassEventArgsToCommand="True">
            </dxmvvm:EventToCommand>
        </dxmvvm:Interaction.Behaviors>
</Canvas >

我正在尝试将画布传递给我的视图模型。

这是我的ViewModel的代码

public DelegateCommand<Canvas> OnSelectedCommand { get; private set; }
        public Vm()
        {
            OnSelectedCommand = new DelegateCommand<Canvas>(OnSelectedEvent, true);
        }
        Canvas c = new Canvas();
        private void OnSelectedEvent(object e)
        {
            if (e == null) return;
            c = e as Canvas;

            // Remove selection on clicking anywhere the window
            if (selected)
            {
                selected = false;
                if (selectedElement != null)
                {
                    // Remove the adorner from the selected element
                    aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
                    selectedElement = null;
                }
            }

            // If any element except canvas is clicked, 
            // assign the selected element and add the adorner
            if (e.Source != myCanvas)
            {
                _isDown = true;
                _startPoint = e.GetPosition(myCanvas);

                selectedElement = e.Source as UIElement;

                _originalLeft = Canvas.GetLeft(selectedElement);
                _originalTop = Canvas.GetTop(selectedElement);

                aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
                aLayer.Add(new ResizingAdorner(selectedElement));
                selected = true;
                e.Handled = true;
            }
        }

目前我的代码不起作用。在后面的代码中,我有方法中需要的MouseButtonEventArgs

我现在面临的问题是如何访问MouseButtonEventArgs

没有它。我无法使用博客中的示例代码

1 个答案:

答案 0 :(得分:2)

您可以使用行为来实现目标。

首先,将以下两个程序集引用添加到项目中:

  

System.Windows.Interactivity.dll

     

Microsoft.Expression.Interactions.dll

添加以下xaml命名空间:

xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

然后为Canvas控件添加行为,如下所示:

    <Canvas>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                <ei:CallMethodAction MethodName="OnLeftButtonClicked" TargetObject="{Binding}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        ...
    </Canvas>

EventTrigger是一种触发行为,可以监听引发的特定事件。 CallMethodAction是一个将调用由TargetObjectMethodName属性指定的方法的操作。这里,TargetObject的值是{Binding},这意味着目标对象是您的视图模型,因此它将从您的视图模型中调用具有此类名称的方法。

然后,在视图模型中,添加以下方法:

    public void OnLeftButtonClicked(object s,MouseButtonEventArgs e)
    {

    }

注意:

  1. 修饰符应为public;

  2. 方法名称应与MethodName的{​​{1}}属性相同;