Is there a way to bind to a dependency property in C# (like XAML does)?
I know I can do change notification, but I was hoping for a way to do a “Two Way” binding. (So that changing my value changes the dependency property.)
Example:
In my User Control View
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); }
}
In My View Model:
// 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;
}
You can do this in C# – you have to build a Binding manually:
For details, see How To: Create a Binding in Code.