将函数绑定到 xaml 中的 MouseDown 事件? [英] Binding a function to the MouseDown event in xaml?

查看:55
本文介绍了将函数绑定到 xaml 中的 MouseDown 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 WPF 和 C# 编程,但我正在努力理解绑定等.

im Working on learning WPF and C# programming at the moment but im struggling with understanding bindings etc.

我被困在将函数或命令绑定"到网格中的 XAML 对象上.

I am stuck on "binding" functions or commands to my XAML object which is in a grid.

<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()}"/>

代码 MouseDown="{Binding Path=ChessPieces[0].Move()}" 不起作用,但显示了我试图实现的内容

The code MouseDown="{Binding Path=ChessPieces[0].Move()}" Does not work but displays what im trying to achive

在我的视图模型中,我定义了一个列表,其中填充了不同棋子的实例,然后我计划将每个图像绑定到该列表中的一个实例.

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.

我现在想做的是绑定 MouseDown 函数,如果我将该函数放在 Mainwindow.xaml.cs 文件中,它似乎可以访问它.但我希望它能够从模型中访问它

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

Mainwindow.xaml.cs

namespace TheGame.Views
{
    public partial class MainWindow : Window   
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ChessBoardViewModel();
        }

    }
}

BlkRook 对象此时已经定义了行、列、名称和函数移动.

The BlkRook object has defined Row, column, name and a function move at the moment.

错误信息:

"'TheGame.Views.MainWindow' 不包含 'Move' 的定义,并且无法找到接受类型为 'TheGame.Views.MainWindow' 的第一个参数的扩展方法 'Move'(您是否缺少 using 指令或程序集参考?)"

"'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?)"

那么..如何将在模型对象中定义的函数绑定到 XAML 对象?

So.. How do i bind a function that is defined in a Model-object to a XAML object?

谢谢

/马丁

推荐答案

您不能绑定到方法.您正在使用 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/

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/

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown" >
            <i:InvokeCommandAction Command="{Binding MouseDownCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

这篇关于将函数绑定到 xaml 中的 MouseDown 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆