【发布时间】:2023-03-23 15:00:01
【问题描述】:
我有一个包含 Telerik RadGridView 的 WPF 用户控件。控件的数据上下文被设置为 MyViewModel 类的一个实例。 MyViewModel 类有一个 myRecords 属性,类型为 ObservableCollection。 RadGridView 是这样绑定的:
ItemsSource="{Binding myRecords}"
在 RadGridView 中,我定义了许多列,其中包含绑定到 MyRecords 属性的 DataTemplates。这工作正常。
我添加了一个包含删除按钮的列。或者更确切地说,它包含一个 DataTemplate,其中包含一个标记为“删除”的按钮。这与记录中定义的命令绑定:
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding deleteCommand}"
CommandParameter="{Binding}">
<Label>Delete</Label>
</Button>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
这很好用。我在 MyRecord 上定义的 ICommand 属性执行。
但事情是这样的——这不是我想要这段代码的地方。我不想在 MyRecord 上运行一个方法,我想在 MyViewModel 上运行一个方法,将适当的 MyRecord 作为参数传递。上面的 CommandParameter="{Binding}" 元素传递了适当的 MyRecord,因此该部分很好。但我无法弄清楚如何将按钮的命令绑定到 MyViewModel 对象上的 ICommand,而不是 MyRecord。
我一直在使用RelativeSource 和AncestorType,但一无所获。
我们将不胜感激。
【问题讨论】:
标签: wpf xaml binding telerik datatemplate