如何在silverlight中实现命令
简单情况:
<usercontrol>
<Views:UserListView x:Name="settingsTreeView"/>
<Button DataContext="{Binding ElementName=settingsTreeView, Path=SelectedItem}"
Command="{Binding CreateChildCommand}"/>
</usercontrol>
任务只是将实现 CreateChildCommand 的 DataContext 绑定到按钮。 DataContext 是树视图中选定的项目。 没什么事发生。 我在运行时检查了按钮属性:按钮的 Command 和 DataContext 属性均为 null。
问题:
- 在运行时更改 DataContext 是否有效?
- 如何将选定的项目传递给 Command 对象?
关于Silverlight中的命令可以参考:
http:// johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/ http://community.infragistics.com/silverlight/media/p/125526。 aspx
http:// houseofbilz.com/archives/2009/05/22/adventures-in-mvvm-commands-in-silverlight/ http://www.silverlightshow.net/items/ Silverlight-4-How-to-Command-Control.aspx
出了问题
Simple case:
<usercontrol>
<Views:UserListView x:Name="settingsTreeView"/>
<Button DataContext="{Binding ElementName=settingsTreeView, Path=SelectedItem}"
Command="{Binding CreateChildCommand}"/>
</usercontrol>
The task just is to bind to button a DataContext which implements CreateChildCommand.
DataContext is the selected item in the treeview.
Nothing to happen.
I have checked the button properties at run time: both Command and DataContext properties of the button are null.
Questions:
- Is it valid to change DataContext at runtime?
- How to pass selected item to the Command object?
Concerning commanding in Silverlight you can refer to:
http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/
http://community.infragistics.com/silverlight/media/p/125526.aspx
http://houseofbilz.com/archives/2009/05/22/adventures-in-mvvm-commands-in-silverlight/
http://www.silverlightshow.net/items/Silverlight-4-How-to-Command-Control.aspx
something goes wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先要做的就是检查您的绑定是否失败。如果是这种情况,您应该在输出日志中看到某种输出。在运行时更改 DataContext 是有效的,如果您希望将所选项目传递给命令,则可以将所选项目绑定到按钮上的 CommandParameter 属性。如果您的输出日志中有错误,请告诉我。
First thing to do would be to check if your bindings are failing. You should see some sort of output in the output log if this is the case. Its valid to change the DataContext at runtime, and you can bind the selected item to the CommandParameter property on the button if you want it to be passed to the command. Let me know if you have errors in your output log or not.