【问题标题】:WPF DataBinding (CommandParameter not working)WPF 数据绑定(CommandParameter 不起作用)
【发布时间】:2012-04-02 03:24:32
【问题描述】:

我的绑定无法正常工作。我的 ActiveView 有一个 ListView (x:Name="MyListView") 以及一个视图模型,因为它是数据上下文。

我有以下 RibbonButton (RibbonControlLibrary):

<r:RibbonButton Label="Update Software" 
    LargeImageSource="/Ganymed.App.UI;component/Images/plain-update.png"
    Command="{Binding ActiveView.DataContext.UpdateSoftwareCommand}"
    CommandParameter="{Binding ActiveView.MyListView.SelectedItems}"/>

命令绑定本身工作正常。但我无法让 CommandParameter 工作。我想将 ActiveView 的 ListView 的选定项目一起发送,以决定是否启用该命令。绑定 CommandParameter 时是否遗漏了什么?

澄清结构:

MainWindow.xaml(包含 RibbonButton,DataContext = MainWindowViewModel)

MainWindowViewModel.cs 包含属性 ActiveView

ActiveView(包含 MyListView,DataContext = ActiveViewViewModel)

ActiveViewViewModel.cs 包含 UpdateSoftwareCommand

任何帮助都会很棒。谢谢

【问题讨论】:

  • 不应该是 ActiveView.MyListView 而不是 ActiveView.ListView,因为你说名字是“MyListView”吗?
  • 嗨,这是我复制代码时的错。我已更改示例中的名称,以免混淆其他人。
  • UpdateSoftwareCommand 的代码是什么样的?如果你使用 DelegateCommand 那么你应该使用 DelegateCommand version
  • 在我们的软件解决方案中,我们有一个 ActionCommand : ICommand 实现,我们将其用作命令。
  • 什么是ActiveView,是UserControl吗?

标签: wpf data-binding command commandparameter


【解决方案1】:

一种可能性:

 CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type r:RibbonButton}}, Path=SelectedItems}"

另外,您可以将一个属性 SelectedItems 添加到您的 ViewModel,绑定它并使其始终保持同步并准备好在命令上进行操作

【讨论】:

  • 您的解决方案不起作用。我猜它不起作用,因为 ListView 不是可视树中按钮的祖先。
  • 抱歉,我的意思是将任何具有绑定到您的 ViewModel 的控件作为 ancenstor
猜你喜欢
  • 1970-01-01
  • 2011-08-17
  • 2010-12-17
  • 1970-01-01
  • 2015-01-18
  • 2011-07-19
  • 2011-03-06
  • 1970-01-01
相关资源
最近更新 更多