WPF用户控件,单击绑定到其他资源... [英] WPF UserControl, Click Binding into other resource...

查看:96
本文介绍了WPF用户控件,单击绑定到其他资源...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是WPF的新手.我在Blend 4中构建了一个复杂的UserControl,它工作正常.

我想将用户控件中任何按钮的单击绑定到c#中的主要资源.

我如何告诉我的用户控件,该点击不在我的用户控件代码中. click事件必须在代码内绑定到我的窗口...

感谢您提供所有答案.

Reinhard

Hello all,

I am new to WPF. I have built a complex UserControl in Blend 4 and it works fine.

I want to bind the click of any buttons in my usercontrol to my main resource in c#.

How can i tell my usercontrol, that the click is not in my code of the usercontrol. The click event must bind to my window inside code...

Thanks for all answers.

Reinhard

推荐答案

在用户控件中暴露一个事件处理程序,并在放置用户控件的窗口中使用它.最好搜索委托.
Expose one event handler in the usercontrol and use it from the window where you placed your usercontrol. Better you search for delegates.


您需要意识到的第一件事是WPF中的命令模型与Winforms开发中的命令模型非常不同.令人困惑的是,它也不会增加命令模式.我花了一些时间来了解新模型.

可以仅仅连接一个click事件处理程序,但这对于可重用性来说是最糟糕的.要实现您想要的功能,您需要创建一个 Command (尽管对于诸如复制粘贴"和打开"之类的常见命令,有些开箱即用"的功能).这些代表将要采取的行动.

在使用命令之前,在视觉树的某个位置,您需要创建命令绑定,这会将命令绑定到要执行的方法.

最后,您需要将命令源(在本例中为按钮)挂接到命令本身.


所有这些都可以在XAML中完成,但是在这里解释太久. MSDN此处具有通常过于复杂的文章:
http://msdn.microsoft.com/en-us/library/ms752308.aspx [ ^ ]
这看起来更有用:

http://www.switchonthecode.com/tutorials/wpf-tutorial-command-绑定和自定义命令 [ ^ ]
The first thing that you need to realise is that the commanding model in WPF is very unlike that in Winforms development. It also, confusingly, does not inplement the command pattern. It took me a while to get my head around the new model.

You can just hook up a click event handler, but this is the worst for re-usability. To implement what you want you need to to create a Command (although there are ones "out the box" for common commands such as Copy & paste and Open). These represent the action that will be taken.

At some point in the visual tree prior to using the command, you need to create a command binding, this binds the command to the methods to be executed.

Finally you need to hook the command source (in this case, the buttons) to the Command itself.


All this can be done in XAML, but would take too long to explain here. MSDN has the usual over-complicated article here:
http://msdn.microsoft.com/en-us/library/ms752308.aspx[^]
This looks more useful:

http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands[^]


这篇关于WPF用户控件,单击绑定到其他资源...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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