【问题标题】:How to implement commanding in silverlight如何在silverlight中实现指挥
【发布时间】:2010-08-20 07:20:37
【问题描述】:

简单案例:

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

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

问题:

  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

出了点问题

【问题讨论】:

    标签: silverlight xaml command datacontext


    【解决方案1】:

    首先要做的是检查您的绑定是否失败。如果是这种情况,您应该在输出日志中看到某种输出。在运行时更改 DataContext 是有效的,如果您希望将所选项目传递给命令,则可以将所选项目绑定到按钮上的 CommandParameter 属性。如果您的输出日志中有错误,请告诉我。

    【讨论】:

    • 你好。没有绑定失败。我尝试打开 SelectedItem 属性的属性更改通知,效果很好。我已将 SelectedItem 设为依赖属性。尽管这不是当前情况下的最佳决定。如果在更改选定项目时没有数据上下文切换,那将是更好的设计。
    猜你喜欢
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2012-01-31
    • 1970-01-01
    • 2012-07-18
    • 2011-03-30
    相关资源
    最近更新 更多