在mvvm中绑定或执行control.method

时间:2014-09-11 14:44:04

标签: wpf mvvm

Textbox有Clear和GetSpellingErrors方法等。
我有可能拥有像这样的东西吗? TextBox Clear = {binding ...} />?

我知道Clear不是一个“依赖”的东西。我正在创作一个用户控件。我可以根据需要添加DependencyProperty,DependencyObject。我只是想知道如何将方法绑定到VM。

PS我不需要Clear的替代品,我知道我可以将属性设置为string.empty。

1 个答案:

答案 0 :(得分:0)

事实证明,你可以使用ICommand并像任何其他DependencyProperty一样创建它。

    public static readonly DependencyProperty CancelCommandProperty = DependencyProperty.Register("CancelCommand", typeof(ICommand), typeof(AddressUserControl), new PropertyMetadata());

    [BindableAttribute(true)]
    public ICommand CancelCommand { get { return (ICommand)GetValue(CancelCommandProperty); } set { SetValue(CancelCommandProperty, value); } }

我仍然遇到的唯一问题是我显然需要将它作为只读绑定。 试图

    DependencyProperty.RegisterReadOnly(...)

但ReadOnly DependencyProperty https://connect.microsoft.com/VisualStudio/feedback/details/540833/onewaytosource-binding-from-a-readonly-dependency-property似乎存在错误