wpf 将函数绑定到 xaml 中的 MouseDown 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20355906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding a function to the MouseDown event in xaml?
提问by Martin
im Working on learning WPF and C# programming at the moment but im struggling with understanding bindings etc.
我目前正在学习 WPF 和 C# 编程,但我正在努力理解绑定等。
I am stuck on "binding" functions or commands to my XAML object which is in a grid.
我被困在将函数或命令“绑定”到网格中的 XAML 对象上。
<Image x:Name="BlkRook1" Source="../Data/BlkRook.png" Grid.Row="{Binding Path=ChessPieces[0].Row}" Grid.Column="{Binding Path=ChessPieces[0].Column}" MouseDown="{Binding Path=ChessPieces[0].Move()}"/>
The code MouseDown="{Binding Path=ChessPieces[0].Move()}"Does not work but displays what im trying to achive
代码MouseDown="{Binding Path=ChessPieces[0].Move()}"不起作用,但显示了我试图实现的目标
In my viewmodel i have defined a list which i have filled up with instances of my diffrent chesspieces, and then im planning on binding each image to a instance from that list.
在我的视图模型中,我定义了一个列表,其中填充了不同棋子的实例,然后我计划将每个图像绑定到该列表中的一个实例。
im trying to do is bind the MouseDown function at the moment, it seems like if i place the function in the Mainwindow.xaml.cs file it can access it. But i want it to be able to access it from the model
我现在想做的是绑定 MouseDown 函数,如果我将该函数放在 Mainwindow.xaml.cs 文件中,它似乎可以访问它。但我希望它能够从模型中访问它
Mainwindow.xaml.cs
主窗口.xaml.cs
namespace TheGame.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ChessBoardViewModel();
}
}
}
The BlkRook object has defined Row, column, name and a function move at the moment.
BlkRook 对象此时已经定义了行、列、名称和一个函数移动。
Error Message:
错误信息:
"'TheGame.Views.MainWindow' does not contain a definition for 'Move' and no extension method 'Move' accepting a first argument of type 'TheGame.Views.MainWindow' could be found (are you missing a using directive or an assembly reference?)"
“'TheGame.Views.MainWindow' 不包含 'Move' 的定义,并且没有可以找到接受类型为 'TheGame.Views.MainWindow' 的第一个参数的扩展方法 'Move'(您是否缺少 using 指令或程序集参考?)”
So.. How do i bind a function that is defined in a Model-object to a XAML object?
那么..如何将在模型对象中定义的函数绑定到 XAML 对象?
Thanks
谢谢
/Martin
/马丁
回答by Florian
You can't bind to methods. You are using MVVM so I would suggest you to use attached properties in combination with commands. You could also use the MouseBinding class: http://msdn.microsoft.com/en-us/library/system.windows.input.mousebinding(v=vs.110).aspx. Commandbinding is explained quite well here: http://www.danharman.net/2011/08/05/binding-wpf-events-to-mvvm-viewmodel-commands/
你不能绑定到方法。您正在使用 MVVM,因此我建议您将附加属性与命令结合使用。您还可以使用 MouseBinding 类:http: //msdn.microsoft.com/en-us/library/system.windows.input.mousebinding(v=vs.110) .aspx。命令绑定在这里解释得很好:http://www.danharman.net/2011/08/05/binding-wpf-events-to-mvvm-viewmodel-commands/
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" >
<i:InvokeCommandAction Command="{Binding MouseDownCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

