【问题标题】:InputBinding CommandParameter Binding to a collectionInputBinding CommandParameter 绑定到集合
【发布时间】:2013-09-22 04:09:50
【问题描述】:

对于 KeyBinding,我正在使用 Binding 在 Xaml 中设置 CommandParameter。在 Binding 中有一个 Converter 设置。

当我将参数绑定到属性 (INPC) 时,绑定系统会使用转换器重新评估。

但是,当我将参数设置为可观察集合时,CollectionChanged 上的绑定系统不会重新评估。因此我收到了初始转换值。

有没有办法触发 CommandParameter 重新评估 CollectionChanged。

    <TextBox Grid.Row="0" Text="{Binding MyParameter}">
      <TextBox.InputBindings>
        <KeyBinding Gesture="CTRL+D" 
                    Command="{Binding MyCommand}" 
                    CommandParameter="{Binding MyParameter, 
                                          Converter={StaticResource converter}}">
        </KeyBinding>
        <KeyBinding Gesture="CTRL+T"
                    Command="{Binding MyCommand}"
                    CommandParameter="{Binding ChangedValuesCollection, 
                                                      Converter={StaticResource CollectionConverter}}">
        </KeyBinding>
      </TextBox.InputBindings>
    </TextBox>

在上面的代码中,我设置了一个视图模型(datacontext)。但我打算将命令参数绑定到 Grid 的 SelectedRows 集合。

【问题讨论】:

  • 你在 ViewModel 上是否有 ChangedValuesCollection?
  • 在这个例子中,在视图模型中。但是我的目标是绑定到网格的 SelectedItems
  • 看看下面的答案对你有没有帮助

标签: wpf key-bindings inputbinding


【解决方案1】:

如果您只想将 DataGrid 的选定项作为命令参数,您可以直接绑定到它。假设 Datagrid 和 Textbox 在同一个可视化树中

 <KeyBinding Gesture="CTRL+T"
                    Command="{Binding MyCommand}"
                    CommandParameter="{Binding SelectedItems, ElementName="myDataGrid"}">

【讨论】:

    猜你喜欢
    • 2011-08-17
    • 2018-11-22
    • 2015-09-10
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2018-10-13
    相关资源
    最近更新 更多