Binding to a WPF DependencyProperty Control in WinForms

I have a WinForms application with some elements that host WPF user controls (using ElementHost).

I want to be able to bind a WinForm ( Button.Enabled) control property to a custom DependencyProperty of a hosted WPF ( SearchResults.IsAccountSelected) user control .

Can I bind a System.Windows.Forms.Binding to a property managed by DependencyProperty?

Also, since I know that the System.Windows.Forms.Binding clock is for events INotifyPropertyChanged.PropertyChanged- will the DependencyProperty-supported property automatically trigger these events, or will I need to implement and control the dispatch of PropertyChanged events manually?

+3
source share
1 answer

DependencyObjectdoes not implement INotifyPropertyChanged, therefore, if you take this route, you will have to implement the dispatch of PropertyChanged events manually.

Fortunately, it DependencyObjecthas a method OnPropertyChanged, so the implementation INotifyPropertyChangedin your- DependencyObjectderived class is trivial, for example:

public class MyClass : HeaderedContentControl, INotifyPropertyChanged
{
  protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  {
    var handler = PropertyChanged;
    if(handler!=null) handler(this, new PropertyChangedEventArgs(e.Property.Name));
    base.OnPropertyChanged(e);
  }
  public event PropertyChangedEventHandler PropertyChanged;
}

I would like echo jsmith to think that binding directly to the UserControl property cannot be the best way. In most cases, MVVM is the best way to go. Of course, there are exceptions.

+1
source

Source: https://habr.com/ru/post/1750016/


All Articles