【问题标题】:Set CommandParameter to ListBoxItem selected event将 CommandParameter 设置为 ListBoxItem 选定事件
【发布时间】:2014-08-07 08:04:11
【问题描述】:

我想将 CommandParameter 传递给 ListBoxItem 选择上的命令,但不知道该怎么做(我是 mvvm & 命令的新手,所以它可能很简单)。

我尝试这样做,但 CommandParameter 属性似乎不可用:

<ListBoxItem>
    <ListBoxItem.CommandBindings>
        <CommandBinding Command="{Binding OperationsCommand}"/>
    </ListBoxItem.CommandBindings>
    <TextBlock Text="All Items"/>
</ListBoxItem>

【问题讨论】:

  • 您知道您实际上可以将所选项目绑定到您的视图模型吗?
  • 确实,这是个好主意。谢谢你。我不会传递参数,而是只获取当前选定的项目。写下这个作为答案,这样我就可以将问题标记为已回答。

标签: wpf mvvm listboxitem commandbinding


【解决方案1】:

您可以直接将所选项目绑定到您的视图模型,并在该项目的设置器中执行您的操作。

<ListBox ItemSource="{Binding YourCollection}" SelectedItem="{Binding MyItem}"/>

【讨论】:

  • +1 或者,可以使用常规命令在模板级别执行此操作,在这种情况下过度杀伤。
猜你喜欢
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
相关资源
最近更新 更多