将命令目标设置为模板部分 [英] Set command target to template part
问题描述
1)具有CommandBindings的自定义DataGrid.
1) Custom DataGrid with CommandBindings.
2)一个RoutedCommand定义.
2) A RoutedCommand Definition.
3)命令目标定义. (XAML)
3) A Command Target Definition. (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的子级(它是Template中的一个元素),从而解决了对此"Custom" DataGrid的需求,而只需要更改常规DataGrid的模板.
I would like to transfer the CommandBindings to a child of my CustomDataGrid (an Element in it's Template) , thus dissolving the need for a this "Custom" DataGrid and only a change in a template of a regular DataGrid.
XAML:CustomDataGridTemplate.
XAML : CustomDataGridTemplate.
<Template TargetType="{x:Type p:CustomDataGrid}" >
......
<p:SomeCustomElement x:Name="I WANT TO BE THE COMMAND TARGET !" />
......
</Template>
我怎样才能做到这一点?是否有向该命令注册SomeCustomElement的过程?
how can i achieve this ? is there a was of registering SomeCustomElement to that command ?
推荐答案
好,所以在所有类型的RoutedCommand旁边,我放置了一些扩展方法来注册其自身类型的路由命令,
Ok , so along side all the type of RoutedCommands i placed a few extension methods to register the routed command with the type it self ,
public static class Commands
{
public static RoutedCommand ClearInputCommand = new RoutedCommand("ClearInputCommand", typeof(Commands));
public static void RegisterCommand(this UIElement uiElement, RoutedCommand command, ExecutedRoutedEventHandler execute, CanExecuteRoutedEventHandler canExecute = null)
{
uiElement.RegisterCommand(new CommandBinding(command, execute, canExecute));
}
public static void RegisterCommand(this UIElement uiElement, CommandBinding commandBinding)
{
CommandManager.RegisterClassCommandBinding(typeof(object), commandBinding);
}
public static void UnRegisterCommand(this UIElement uiElement, RoutedCommand command)
{
for (int i = 0; i < uiElement.CommandBindings.Count; i++)
{
CommandBinding c = uiElement.CommandBindings[i];
if (c.Command == command)
{
uiElement.CommandBindings.RemoveAt(i);
}
}
}
public static void UnRegisterCommand(this UIElement uiElement, CommandBinding commandBinding)
{
uiElement.CommandBindings.Remove(commandBinding);
}
}
然后从该类的构造函数中调用它,我不确定是否需要注销这个棘手的问题,在我看来这可能会导致内存泄漏,因为它包含对Execute和CanExecute委托的引用.
and then just called it from the constructor of that class , i'm not sure if i need to unregister this tough it seems to me that this can cause memory leaks , since it holds a reference to the Execute and CanExecute delegates.
为了注销它们,我将必须跟踪所有已注册的uielements并在应用程序关闭时清除CommandBindings.
in order to Unregister them i would have to keep track of all the registered uielements and clear there CommandBindings on application shut down.
我认为更好的解决方案是使用诸如CompositesCommand之类的棱镜.但这现在就可以了.
i think a better solution would be to use something like prisms CompositeCommand. but this would do for now.
这篇关于将命令目标设置为模板部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!