在 WPF 用户控件中公开多个命令
我希望有一个更好的解释:
- 我有一个带有 3 个按钮的工具栏,所有三个按钮都绑定到一个命令(包括命令参数)
- 此工具栏在多个屏幕上使用
- 所有屏幕上完全相同
工具栏的 xaml 在我想要的 删除工具栏实例并将其替换为提供 3 个命令的用户控件,这样我就可以在每个屏幕中保留绑定。 计划稍后更改工具栏功能,但外部编程接口(即 3 个命令)是相同的。
因此:
- 我创建了一个用户控件,并为每个命令创建了 3 组依赖属性(OneCommand、OneCommandParameter、OneCommandTarget),以便我可以将它们用于绑定。
- 我将工具栏 xaml 移至用户控件 xaml 内。
- 我修改了工具栏按钮上的绑定,以绑定到
- 每个屏幕上的固有用户控件属性(或者实际上,现在只是第一个),我用用户控件替换了原始工具栏,将新属性绑定到正确的命令。
控件显示,但按钮不起作用。 就是这样。
-
原始解释 - 不太清楚:
我有一个 WPF 用户控件封装了许多按钮。 以前,该控件是一个带有许多按钮的工具栏,但由于我需要在多个屏幕上提供完全相同的功能,因此我将工具栏重构为自定义控件。
但是,我想保留原始按钮的命令绑定。
- 我在用户控件上创建了 3 组依赖属性(XCommand、XCommandParameter 和 XCommandTarget)。
- 在用户控件 xaml 中,我将“真实”按钮绑定到这些属性(每个按钮绑定到每组属性)。
- 在使用用户控件的地方,我将新属性绑定到真实的命令绑定。
本质上,我想保留用户控件公开的每个“命令”的 ICommandSource 功能。 但是,这种双重数据绑定方案似乎不起作用,或者我做错了什么。 :)
有一个更好的方法吗? 我需要的只是将命令从控件外部“桥接”到内部按钮,以便保留 Execute 和 CanExecute 功能。
A better explanation, I hope:
- I have a toolbar with 3 buttons on it, all three bound to a Command (including a CommandParameter)
- this toolbar is used on several screens
- the xaml of the toolbar is exactly the same over all those screens
I want to remove the toolbar instance and replace it with a user control that provides 3 commands, so I can keep the bindings in each screen. The plan is to later change the toolbar functionality, but the external programming interface (namely, 3 commands) is the same.
So:
- I created a user control, and created 3 sets of dependency properties for each command (OneCommand, OneCommandParameter, OneCommandTarget) so I can use these for the binding.
- I moved the toolbar xaml inside the user control xaml.
- I modified the bindings on the toolbar buttons to bind to the intristic user control properties
- on each screen (or really, only the first for now) I replaced the original toolbar with the user control,binding the new properties to the correct commands.
The control shows, but the buttons don't work.
That's about it.
--
Original explanation - not so clear:
I have a WPF user control encapsulating a number of buttons. Previously, the control was a Toolbar with a number of buttons on it, but since I need exact the same functionality on a number of screens, I refactored the toolbar into a custom control.
However, I'd like to keep the command bindings of the original buttons.
- I created 3 sets of dependency properties (XCommand, XCommandParameter and XCommandTarget) on the usercontrol.
- In the user control xaml I bind the "real" buttons to those properties (each button to each set of properties).
- Where I use the usercontrol, I bind the new properties to the real command bindings.
In essence, I want to keep the ICommandSource functionality for each "command" that the user control exposes. However, this dual databinding scenario doesn't seem to work, or I'm doing something wrong. :)
Is there a better way to do this? All I need is to "bridge" the commands from outside the control to the inner buttons so the Execute and CanExecute functionality remains.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题。 我的内部控制绑定中的RelativeSource 有一个错误。 现在,它按预期工作得很好。
I solved this. There was a bug in my RelativeSource in the internal control bindings. It works fine as expected, now.