MVVM 中的 Silverlight UserControl 与 RelayCommand- 例子?
有人有一个很好的例子来创建用户控件,然后将命令绑定到它吗?
我的问题是将命令转发到 UserControl 中的控件,例如 TreeView 的 Drop 事件。目前尚不清楚,我该怎么做。
两个答案都很有趣,谢谢,他们有帮助,但是...
我想要这样的东西:
用法:
<my:MyControl Command="{Binding XCommand}" CommandParameter="{Binding [Here what?]}"/>
控件包含两个 DependencyProperties、Command 和 CommandParameters,我想将这两个 DependecyProperties 绑定到 TreeView - Drop 事件。我怎样才能做到这一点?因为 CommandParameter 的用法是相反的:不是控件传递参数,而是用户想要一些东西: CommandParameter="{Binding Text, ElementName=DisableCommandTextBox}"
Has somebody a good example to create a UserControl, and then bind commands to it?
My problem is to forward the command to a control in the UserControl, for example to a TreeView's Drop event. It's not clear, how can I do that.
Both answer are interesting and thanks, they help, but...
I want something like that:
The usage:
<my:MyControl Command="{Binding XCommand}" CommandParameter="{Binding [Here what?]}"/>
The control contains the two DependencyProperties, Command and CommandParameters, and I'd like to bind these two DependecyProperties to a TreeView - Drop event. How can I do that? Because the usage of the CommandParameter is reversed: not the control passes a parameter, but the users wants something: CommandParameter="{Binding Text, ElementName=DisableCommandTextBox}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试将事件转发到您的视图模型。我想您不需要 RelayCommand,但您需要 EventToCommand。我不知道如何绑定树视图事件,但你总是可以尝试一下。
使用 MVVM Light 的示例:
http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx
You are trying to forward an event to your viewmodel. I guess you don't need RelayCommand but you will need EventToCommand. I don't know about binding the treeview events, but you can always give it a try.
Example using MVVM Light:
http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx
不幸的是,由于 TreeView 的限制,我们无法将视图模型中的 RelayCommand 直接绑定到 Drop 事件。
不过,这个问题之前已经解决了。看看这个链接 看看它是如何完成的。如果您想要一个更典型的命令绑定示例(可能是按钮),请在下面留下评论,我会将其添加进去。
Unfortunately, we cannot bind a RelayCommand within a viewmodel directly to the Drop event due to the limitations on TreeView.
However, this problem has been previously solved. Take a look at this link to see how it was done. If you'd like an example of a more typical command binding (to a button perhaps), leave a comment below and I'll add that in.
我只需保留 CommandParameter,然后使用适当的参数在 SelectedItemChanged 事件处理程序中调用该命令。
I just had to leave the CommandParameter, and I call the command in the SelectedItemChanged event handler with the appropiate parameter.