【问题标题】:Set command target to template part将命令目标设置为模板部分
【发布时间】: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


    【解决方案1】:

    好的,除了所有类型的 RoutedCommands 我放置了一些扩展方法来注册路由命令的类型,

    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 委托的引用。

    为了取消注册它们,我必须跟踪所有已注册的 uielements 并在应用程序关闭时清除 CommandBindings。

    我认为更好的解决方案是使用棱镜 CompositeCommand 之类的东西。但现在就可以了。

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2011-04-21
      • 2011-08-20
      • 2013-04-03
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多