[英]How to get mouse position when using XamlBehaviorsWpf and MVVM (Formerly Interactivity)
Now that XamlBehaviorsWpf [Tag missing, but I don't have the rights to add] has replaced System.Windows.Interactivity
in Blend SDK which is deprecated I'm struggling to figure out how to use it to get Mouse position on a canvas.
我认为下面的MouseX
和MouseY
是旧的Interactivity
语法,但我似乎找不到XamlBehaviorsWpf
等效项。 因此,使用这些属性会产生编译错误。
Click
方法正在工作,并以其默认值 999,999 返回坐标。
有谁知道我如何使用XamlBehaviorsWpf
?
XAML:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="800" Background="Black">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="MouseLeftButtonDown">
<behaviors:CallMethodAction TargetObject="{Binding}" MethodName="Click"/>
</behaviors:EventTrigger>
<behaviors:EventTrigger EventName="MouseMove">
<behaviors:ChangePropertyAction MouseX="{Binding X, Mode=OneWayToSource}"/>
<behaviors:ChangePropertyAction MouseY="{Binding Y, Mode=OneWayToSource}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
视图模型
public void Click(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(X.ToString() + ","+ Y.ToString());
}
private double x = 999;
public double X
{
get { return x; }
set { SetAndNotify(ref this.x, value); }
}
private double y = 999;
public double Y
{
get { return y; }
set { SetAndNotify(ref this.y, value); }
}
我的很多问题似乎都源于一个错字,已在问题中得到纠正(感谢安迪)。 这里有一篇易于理解的文章,我曾经让它工作。 您基本上可以将所有System.Windows.Interactivity
引用与XamlBehaviorsWpf
交换,它的工作原理都是一样的。
我的工作代码如下以便于参考,或者如果上面的链接消失:
XAML
<UserControl x:Class="Drain.Views.Cad.CadView"
x:Name="cad"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Drain.Views.Cad"
xmlns:vm="clr-namespace:Drain.ViewModels.Cad"
xmlns:cad="clr-namespace:Drain.Models.Cad"
xmlns:s="https://github.com/canton7/Stylet"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<behaviors:Interaction.Behaviors>
<behaviors:FluidMoveBehavior AppliesTo="Children" />
</behaviors:Interaction.Behaviors>
<ItemsControl x:Name="MainPanel">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="800" Background="Black">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="MouseLeftButtonDown">
<behaviors:CallMethodAction TargetObject="{Binding}" MethodName="Click"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
<behaviors:Interaction.Behaviors>
<vm:MouseBehaviour MouseX="{Binding X, Mode=OneWayToSource}"
MouseY="{Binding Y, Mode=OneWayToSource}" />
</behaviors:Interaction.Behaviors>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=OneWay}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=OneWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</UserControl>
视图模型
public void Click(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(X.ToString() + ","+ Y.ToString());
}
private double x = 999;
public double X
{
get { return x; }
set { SetAndNotify(ref this.x, value); }
}
private double y = 999;
public double Y
{
get { return y; }
set { SetAndNotify(ref this.y, value); }
}
鼠标行为 Class
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;
namespace Drain.ViewModels.Cad
{
public class MouseBehaviour : Behavior<Panel>
{
public static readonly DependencyProperty MouseYProperty = DependencyProperty.Register(
"MouseY", typeof(double), typeof(MouseBehaviour), new PropertyMetadata(default(double)));
public static readonly DependencyProperty MouseXProperty = DependencyProperty.Register(
"MouseX", typeof(double), typeof(MouseBehaviour), new PropertyMetadata(default(double)));
public double MouseY
{
get { return (double)GetValue(MouseYProperty); }
set { SetValue(MouseYProperty, value); }
}
public double MouseX
{
get { return (double)GetValue(MouseXProperty); }
set { SetValue(MouseXProperty, value); }
}
protected override void OnAttached()
{
AssociatedObject.MouseMove += AssociatedObjectOnMouseMove;
}
private void AssociatedObjectOnMouseMove(object sender, MouseEventArgs mouseEventArgs)
{
var pos = mouseEventArgs.GetPosition(AssociatedObject);
MouseX = pos.X;
MouseY = pos.Y;
}
protected override void OnDetaching()
{
AssociatedObject.MouseMove -= AssociatedObjectOnMouseMove;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.