简体   繁体   English

在WPF用户控件中公开多个命令

[英]Expose multiple command in WPF user control

A better explanation, I hope: 我希望有一个更好的解释:

  • I have a toolbar with 3 buttons on it, all three bound to a Command (including a CommandParameter) 我有一个带有3个按钮的工具栏,所有三个按钮都绑定到Command(包括CommandParameter)
  • this toolbar is used on several screens 此工具栏用于多个屏幕
  • the xaml of the toolbar is exactly the same over all those screens 在所有这些屏幕上,工具栏的xaml完全相同

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. 我想删除工具栏实例,并用提供3个命令的用户控件替换它,以便可以将绑定保留在每个屏幕中。 The plan is to later change the toolbar functionality, but the external programming interface (namely, 3 commands) is the same. 计划以后要更改工具栏的功能,但是外部编程接口(即3个命令)是相同的。

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. 我创建了一个用户控件,并为每个命令(OneCommand,OneCommandParameter,OneCommandTarget)创建了3组依赖项属性,以便可以将它们用于绑定。
  • I moved the toolbar xaml inside the user control xaml. 我将工具栏xaml移到了用户控件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. 我有一个WPF用户控件,其中封装了许多按钮。 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. 我在用户控件上创建了3组依赖项属性(XCommand,XCommandParameter和XCommandTarget)。
  • In the user control xaml I bind the "real" buttons to those properties (each button to each set of properties). 在用户控件xaml中,我将“真实”按钮绑定到那些属性(每个按钮绑定到每组属性)。
  • Where I use the usercontrol, I bind the new properties to the real command bindings. 在使用usercontrol的地方,我将新属性绑定到实际的命令绑定。

In essence, I want to keep the ICommandSource functionality for each "command" that the user control exposes. 本质上,我想为用户控件公开的每个“命令”保留ICommandSource功能。 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. 我所需要的只是将命令从控件外部“桥接”到内部按钮,以便保留Execute和CanExecute功能。

I solved this. 我解决了 There was a bug in my RelativeSource in the internal control bindings. 我的RelativeSource中的内部控制绑定中有一个错误。 It works fine as expected, now. 现在,它可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM