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

发布于 12-07 21:59 字数 922 浏览 0 评论 0原文

假设如下代码,

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。

编辑
我可以在 SomeCustomControl 中使用 DataContext SomeView。两者之间有更多的逻辑和分离,我无法消除。我“必须”在我的 SomeCustomControl 中的某处维护重新加载/保存 ICommands 的引用。

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.

EDIT
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个2024-12-14 21:59:41

让我直接告诉你,你想绑定到 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}" /> 

Let me get you straight, you want to bind to the Reload and Save right?

So that needs creating, declaring and defining two dependency properties ReloadCommandProperty and SaveCommandProperty of type ICommand for SomeCustomControl.

So assuming that SomeCustomControl derives from 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 RelodCommand and SaveCommand properties will start working...

     <SomeCustomControl RelodCommand="{Binding ViewModelReloadCommand}"
                        SaveCommand="{Binding ViewModelSaveCommand}" /> 
内心旳酸楚2024-12-14 21:59:41

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

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

将代码中的绑定更改为“

<Button Command={Binding ReloadCommand}" />

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文