【问题标题】:How to create bindable commands in Custom control?如何在自定义控件中创建可绑定命令?
【发布时间】:2011-12-01 09:53:18
【问题描述】:

假设如下代码,

public class SomeViewModel{
      ICommand ReloadCommand{get...}
      ICommand SaveCommand{get..}
}

//SomeView.xaml
<SomeCustomControl Reload="ReloadCommand" Save="SaveCommand" /> //NOT SURE HOW??

//SomeCustomContro.xaml
<SomeCustomControl x:Name="someCustomControl">
<Button Command={Binding ElementName=someCustomControl, Path=Reload />
<Button Command={Binding ElementName=someCustomControl, Path=Save />
</SomeCustomControl>

//SomeCustomControl.xaml.cs
.....  //NOT SURE HOW TO ALLOW BINDING TO A ICOMMAND ??

在我的 SomeCustomControl 中,我需要支持“在 xaml 中绑定 ICommand”。 我知道 DependencyProperties 可以像这样绑定,但在这种情况下我需要绑定 ICommand。

有人可以建议最好的方法吗? 任何建议的材料或链接都会有用,因为我缺少方向。

编辑 1) 我可以在 SomeCustomControl 中使用 DataContext SomeView。两者之间有更多我无法化解的逻辑和分离。我“必须”在 SomeCustomControl 中的某处维护 Reload/Save ICommands 的引用。

非常感谢。

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    让我直说,你想绑定到ReloadSave 对吗?

    因此需要为SomeCustomControl 创建、声明和定义ICommand 类型的两个依赖属性ReloadCommandPropertySaveCommandProperty

    所以假设SomeCustomControl 派生自Control ...

    public class SomeCustomControl : Control
    {
        public static DependencyProperty ReloadCommandProperty
            = DependencyProperty.Register(
                "ReloadCommand",
                typeof (ICommand),
                typeof (SomeCustomControl));
    
        public static DependencyProperty SaveCommandProperty
            = DependencyProperty.Register(
                "SaveCommand",
                typeof(ICommand),
                typeof(SomeCustomControl));
    
        public ICommand ReloadCommand
        {
            get
            {
                return (ICommand)GetValue(ReloadCommandProperty);
            }
    
            set
            {
                SetValue(ReloadCommandProperty, value);
            }
        }
    
        public ICommand SaveCommand
        {
            get
            {
                return (ICommand)GetValue(SaveCommandProperty);
            }
    
            set
            {
                SetValue(SaveCommandProperty, value);
            }
        }
    }
    

    正确绑定到RelodCommandSaveCommand 属性后将开始工作...

         <SomeCustomControl RelodCommand="{Binding ViewModelReloadCommand}"
                            SaveCommand="{Binding ViewModelSaveCommand}" /> 
    

    【讨论】:

      【解决方案2】:

      创建一个将返回您的命令的属性,并在需要的地方绑定该属性。

      private ICommand _reloadCommand;
      public ICommand ReloadCommand
      {
        get 
        { 
          if(_reloadCommand == null) _reloadCommand = CreateReloadCommand();
          return _reloadCommand;
        }
      }
      

      将代码中的绑定更改为

      <Button Command={Binding ReloadCommand}" />
      

      并将自定义控件 DataContext 绑定到包含命令的视图模型。

      【讨论】:

      • 你的意思是我可以直接将 ICommand 实例绑定到控件中的属性吗?另外,什么是“CreateReloadCommand”?该命令实例将在 ViewModel 中创建,而 SomeCustomControl 只需要存储该实例引用(使用 xaml 绑定)。
      • 你可以将 ICommand 类型的值绑定到任何你想要的值,绑定的最有意义的依赖属性是ButtonBase.CommandPropertyCreateReloadCommand 是一种创建命令的方法 - 我不想编写命令创建和初始化代码。当我输入我的答案时,您的代码中有命令方法(您稍后更改了它们)。
      猜你喜欢
      • 1970-01-01
      • 2012-02-09
      • 2019-03-09
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多