如何在自定义控件中创建可绑定命令? [英] How to create bindable commands in Custom control?
问题描述
假设代码如下,
public class SomeViewModel{ICommand ReloadCommand{get...}ICommand SaveCommand{get..}}//SomeView.xaml<SomeCustomControl Reload="ReloadCommand" Save="SaveCommand"/>//不确定如何??//SomeCustomContro.xaml<SomeCustomControl x:Name="someCustomControl"><Button Command={Binding ElementName=someCustomControl, Path=Reload/><Button Command={Binding ElementName=someCustomControl, Path=Save/></SomeCustomControl>//SomeCustomControl.xaml.cs.....//不确定如何允许绑定到 ICOMMAND ??
在我的 SomeCustomControl 中,我需要支持在 xaml 中绑定 ICommand".我知道 DependencyProperties 可以像这样绑定,但在这种情况下,我需要绑定 ICommand.
有人可以建议最好的方法是什么吗?任何建议的材料或链接都会有用,因为我缺少方向.
编辑 1) 我可以在 SomeCustomControl 中使用 DataContext SomeView.两者之间有更多的逻辑和分离,我无法化解.我必须"在我的 SomeCustomControl 的某处维护一个 Reload/Save ICommands 的引用.
非常感谢.
让我直说,你想绑定到 Reload
和 Save
对吗?>
因此需要为 SomeCustomControl
创建、声明和定义 ICommand
类型的两个依赖属性 ReloadCommandProperty
和 SaveCommandProperty
.
所以假设 SomeCustomControl
派生自 Control
...
public class SomeCustomControl : Control{公共静态 DependencyProperty ReloadCommandProperty= DependencyProperty.Register("重新加载命令",类型(ICommand),typeof (SomeCustomControl));公共静态 DependencyProperty SaveCommandProperty= DependencyProperty.Register("保存命令",类型(ICommand),typeof(SomeCustomControl));公共 ICommand 重新加载命令{得到{返回(ICommand)GetValue(ReloadCommandProperty);}放{SetValue(ReloadCommandProperty, value);}}公共 ICommand 保存命令{得到{返回(ICommand)GetValue(SaveCommandProperty);}放{SetValue(SaveCommandProperty, value);}}}
在正确绑定到 RelodCommand
和 SaveCommand
属性后,将开始工作...
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.
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.
Thanks a lot.
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}" />
这篇关于如何在自定义控件中创建可绑定命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!