【发布时间】:2014-02-02 14:23:48
【问题描述】:
1) 带有 CommandBindings 的自定义 DataGrid。
2) RoutedCommand 定义。
3) 命令目标定义。 (XAML)
CS:
//(1)
public class CustomDataGrid : DataGrid
{
this.CommandBindings.Add(new CommandBinding(Commands.ClearInputCommand,
ClearFilter, ClearFilterCanExecute));
}
//(2)
public static class Commands
{
public static RoutedCommand ClearInputCommand = new RoutedCommand("ClearInputCommand", typeof(Commands));
}
XAML:
<!-- (3) -->
<local:CustomDataGrid x:Name="grid" />
<Button Command="{x:Static p:Commands.ClearInputCommand}"
CommandTarget="{Binding ElementName=grid}"/>
我想将 CommandBindings 转移到我的 CustomDataGrid 的子级(模板中的一个元素),从而消除了对这个“自定义”DataGrid 的需求,并且只需要更改常规 DataGrid 的模板。
XAML:自定义数据网格模板。
<Template TargetType="{x:Type p:CustomDataGrid}" >
......
<p:SomeCustomElement x:Name="I WANT TO BE THE COMMAND TARGET !" />
......
</Template>
我怎样才能做到这一点?是否有将 SomeCustomElement 注册到该命令?
【问题讨论】:
标签: wpf routed-commands