没有按钮的 MVVM 命令
我想启动绑定命令,但不想在 UserControl
上使用 Button
。实际上,UserControl
本身就是一个Button
。当单击其表面上的任意位置时,我想启动绑定命令。有没有办法给UserControl
一个命令?
旁注:一个命令用于一个控件,并且只有几个特定的开箱即用控件?这一切看起来有点笨拙。我开始认为 MVVM 不切实际。我可以通过接口和 OOP 很好地解耦我的 UI。无论如何,我还是有希望的。
另外,我不愿意破解任何东西或使用昂贵的解决方法。如果我做不到这一点,我就会放弃 MVVM。
I want to initiate a bound command, but I don't want to use a Button
on my UserControl
. In effect the UserControl
is itself a Button
. When it is clicked anywhere on its surface I want to initiate a bound command. Is there a way to give a UserControl
a command?
In a side note: one command for one control and only a few certain out-of-the-box controls? This all seems a little clunky. I'm starting to think that MVVM is impractical. I can decouple my UI just fine with Interfaces and OOP. Anyway, I still have hope.
Also, I'm not willing to hack anything or use an expensive workaround. If I can't do this, I'm abandoning MVVM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在此处查看 ICommandSource 界面:http:// msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.aspx。如果你想让一个控件有一个命令,那么你的控件应该实现这个接口。实现此接口的控件的示例是 ButtonBase 和 MenuItem。希望这有帮助。
Take a look at the ICommandSource interface here: http://msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.aspx. If you want a control to have a command, then your control should implement this interface. Examples of controls that implement this interface are ButtonBase and MenuItem. Hope this helps.
如果您的 UserControl 本质上是一个 Button,那么您为什么要编写自己的 UserControl 而不是使用 Button 类?
要添加更多信息,您可以执行以下操作:
public class MyCoolButton : Button { }
MyCoolButton
的样式 - 不要命名该样式,以便它适用于所有 MyCoolButtonIf your UserControl is essentially a Button, why are you writing your own UserControl instead of using the Button class?
To add more info, here's what you do:
public class MyCoolButton : Button { }
MyCoolButton
- don't name the style so it applies to all MyCoolButtons我同意保罗·贝茨的观点。
我经常使用按钮作为顶部容器来创建自己的 ListBoxItemContainerStyle,其中除了无属性的内容呈现器之外什么都没有。这使我可以使用按钮功能(如命令),而无需安装 Windows chrome。
将其放入 ListBoxItemContainerStyle 中还可以让我在单击它时不会显示正常的虚线边框 (FocusVisualStyle={x:Null})。
您是否使用 Visual Studio 或 Expression Blend 进行样式设计?
此外,一些 MVVM 框架提供了一个接口,用于添加命令式的功能来控制除按钮之外的控件。 Caliburn 有相当丰富的命令模式。但是,我不确定它是否允许在非按钮控件上绑定命令。
I will agree with Paul Betts.
Quite often I create my own ListBoxItemContainerStyle using a button as the top container with nothing but a propertyless content presenter in it. This allows me to use the buttons functionality (like Command) without having the Windows chrome on it.
Putting it in the ListBoxItemContainerStyle also lets me make it so that when it is clicked it does not display the normal dotted border (FocusVisualStyle={x:Null}).
Are you using Visual Studio or Expression Blend to do your styling?
Additionally, some MVVM frameworks provide an interface for adding a command-ish ability to controls other than buttons. Caliburn has a pretty rich command pattern. I am not sure if it allows binding commands on non-button controls, however.
OP 要求提供一个示例,说明如何使用按钮控件,但内容正确填充整个按钮。您可以使用 ContentAlignment 属性来执行此操作:
这将创建一个带有两个使用网格控件间隔的标签的按钮。我将网格标记为关闭 HitTestVisible,因为您必须决定哪些控件应该像按钮一样交互,哪些控件应该像控件一样交互。例如,您可能有一个嵌入式 TextBox,您希望无需单击按钮即可单击该 TextBox,在这种情况下,它应该具有 HitTestVisible=True。
The OP asked for an example of how you could use a button control, but with the content properly filling the entire button. You can do this using the ContentAlignment properties:
This creates a button with two labels spaced using a Grid control. I mark the Grid to turn off HitTestVisible, as you have to decide which controls should interact like the button and which should interact like controls. For instance, you might have an embedded TextBox that you want to be clickable without clicking the button, in which case it should have HitTestVisible=True.
WPF 支持图层和透明度:
您可以创建任何支持在高级透明图层上进行命令的任何内容(您想要的大小),以充当按钮。
WPF supports layers and transparency :
You can create anything that supports commanding on a superior transparent layer, the size you want, to act as a button.