有没有办法绑定到 C# 中的依赖属性(就像 XAML 一样)?
我知道我可以做更改通知,但我希望有一种方法来进行“双向”绑定。(因此更改我的值会更改依赖项属性。)
例子:
在我的用户控制视图中
public static readonly DependencyProperty IsRequiredProperty =
DependencyProperty.Register("IsRequired", typeof(bool),
typeof(MyUserControl), new FrameworkPropertyMetadata(default(bool)));
public bool IsRequired
{
get { return (bool)GetValue(IsRequiredProperty); }
set { SetValue(IsRequiredProperty, value); }
}
在我的视图模型中:
// This is the one I want bound to the dependency property.
bool IsRequired { //INotifyPropertyChanged getter and setter}
public void SomeCommandExec(Object obj)
{
// Update the dependency property by doing this:
IsEnabled = False;
}