WPF:如何绑定到控件中定义的ICommand?

Jon*_*len 0 wpf

我的控件有一个返回ICommand对象的属性.使用XAML,如何将按钮绑定到它?

DHN*_*DHN 5

如果不知道两者的关系,我会使用元素绑定.例如

<YourControl x:Name="CmdSrc" />
<Button Command={Binding ElementName=CmdSrc, Path=CmdProperty} />
Run Code Online (Sandbox Code Playgroud)

您应该考虑控件提供命令的方法.在我看来有点奇怪.;)

问候

编辑

好的,这只是一个暗示.以防你没有考虑它.

这是绑定命令的另一种方法.我承认我没有测试它.但我认为以下内容也应该有效.

Button在控件中时,您也可以使用相对绑定.

<YourControl>
  <Button Command={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourControl}}, Path=CmdProperty} />
</YourControl>
Run Code Online (Sandbox Code Playgroud)

然后,您不需要控件的名称.我正在避免使用名字,以防止代码背后出现肮脏的变通方法.

是的,我知道这是某种偏执狂.;)