如何在silverlight中实现命令

发布于 2024-09-15 09:36:49 字数 1367 浏览 0 评论 0原文

简单情况:

<usercontrol>
   <Views:UserListView x:Name="settingsTreeView"/>
   <Button DataContext="{Binding ElementName=settingsTreeView, Path=SelectedItem}"
           Command="{Binding CreateChildCommand}"/>
</usercontrol>

任务只是将实现 CreateChildCommand 的 DataContext 绑定到按钮。 DataContext 是树视图中选定的项目。 没什么事发生。 我在运行时检查了按钮属性:按钮的 Command 和 DataContext 属性均为 null。

问题:

  1. 在运行时更改 DataContext 是否有效?
  2. 如何将选定的项目传递给 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:

  1. Is it valid to change DataContext at runtime?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光瘦了 2024-09-22 09:36:49

首先要做的就是检查您的绑定是否失败。如果是这种情况,您应该在输出日志中看到某种输出。在运行时更改 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文