wpf 如何在自定义控件中创建可绑定命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7660547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:06:29  来源:igfitidea点击:

How to create bindable commands in Custom control?

wpfxamlbinding

提问by Manish Basantani

Assuming a code like below,

假设代码如下,

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 ??

In my SomeCustomControl, I need to support "binding of ICommand in xaml". I understand DependencyProperties could be bind like this, but in this case I need to bind ICommand.

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

Can somebody please suggest what is the best way to do this? Any suggested material or link would be of use because I am missing a direction.

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

EDIT 1) I can use the DataContext SomeView in SomeCustomControl. There is more logic and separation between the two which I can not dissolve. I 'must' maintain a reference of Reload/Save ICommands somewhere in my SomeCustomControl.

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

Thanks a lot.

非常感谢。

回答by WPF-it

Let me get you straight, you want to bind to the Reloadand Saveright?

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

So that needs creating, declaring and defining two dependency properties ReloadCommandPropertyand SaveCommandPropertyof type ICommandfor SomeCustomControl.

所以这需要创建、声明和定义两个依赖属性ReloadCommandPropertySaveCommandProperty类型ICommandSomeCustomControl.

So assuming that SomeCustomControlderives from Control...

所以假设它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);
        }
    }
}

After this proper binding to RelodCommandand SaveCommandproperties will start working...

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

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

回答by Karel Frajták

Create a property that will return your command and bind this property wherever needed.

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

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

Change the binding in your code to

将代码中的绑定更改为

<Button Command={Binding ReloadCommand}" />

And bind the custom control DataContext to the view model that contains the commands.

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