我还是MVVM的新手,但是我试图学习它。
我需要将绘制的矩形调整到我的画布中。但我想用MVVM方式做到这一点。
我发现这篇文章及其工作,它满足了我的需求,但它不是MVVM
我正在尝试将此代码转换为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
?
答案 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
是一个将调用由TargetObject
和MethodName
属性指定的方法的操作。这里,TargetObject
的值是{Binding}
,这意味着目标对象是您的视图模型,因此它将从您的视图模型中调用具有此类名称的方法。
然后,在视图模型中,添加以下方法:
public void OnLeftButtonClicked(object s,MouseButtonEventArgs e)
{
}
注意:
修饰符应为public
;
方法名称应与MethodName
的{{1}}属性相同;