[英]WPF MouseMove on Grid, update Position on Object Instance
I want to bind the mousemove event to an instance of an object, and then update some properties in this object, how can I go about doing this? 我想将mousemove事件绑定到一个对象的实例,然后更新该对象中的某些属性,我该怎么做呢? Below is my XAML: 以下是我的XAML:
<Window.Resources>
<h:AdaptiveObject x:Key="adaptiveObject" />
</Window.Resources>
<Grid Name="Container"
MouseMove="{Binding Source={StaticResource adaptiveObject}, Path=UpdateMouse}"
And here is the current C# I have, which is just concept and doesn't work, just want to show you what i'm attempting: 这是我目前使用的C#,这只是概念,无法正常工作,只想向您展示我在尝试什么:
namespace AdaptiveViewport
{
public class AdaptiveObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public int MouseX { get; set; }
public int MouseY { get; set; }
public UpdateMouse(object sender, MouseEventArgs e)
{
MouseX = e.X;
MouseY = e.Y;
}
Since MouseMove is a Event you cant simply bind it to a Command. 由于MouseMove是事件,您不能简单地将其绑定到命令。 One way to achieve what you want would be to use a method in your code-behind: 实现所需目标的一种方法是在代码隐藏中使用一种方法:
<Grid MouseMove="OnMouseMove" Name="Container">...</Grid>
Code Behind: 背后的代码:
private void OnMouseMove(object sender, MouseEventArgs e)
{
var p = Mouse.GetPosition(Container);
// p.X and p.Y are your coordinates here
}
You can probably also use blend interactivity features but i couldn't figure out how to pass the coordinates to a bound command. 您可能还可以使用混合交互功能,但是我不知道如何将坐标传递给绑定命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.