Please note that this does not answer the questions (I should have read better). If you are interested in using the WPF control in a WinForms application, then this is the approach. My scenario: 1) Have a WinForms control that is used in many places in my application. 2) Want to develop a WPF implementation that will use the MVVM pattern. 3) Want to write a control as the right WPF control complete with dependent properties so that it can be used correctly when my application, ultimately, all WPF. 4) You want the same WinForms control and API not to violate the existing client code in my application.
In most cases, everything was simple, except that my WinForms managed events when changing the properties of my WPF control. I wanted to use binding, but since the source of the binding should be DependencyObject, and System.Windows.Forms.UserControl should not, I had to create a simple nested class. I wrote my WPF control in the same way as if I integrated it into a WPF application and just did some extra thunking to get my WinForms shell working.
Here is the code for my WPF control:
public partial class MonkeySelector : UserControl { public static readonly DependencyProperty SelectedMonkeyProperty = DependencyProperty.Register( "SelectedMonkey", typeof(IMonkey), typeof(MonkeySelector)); public MonkeySelector() { InitializeComponent(); } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e);
Here is the code for managing WinForms:
public partial class WinFormsMonkeySelector : UserControl { public event EventHandler SelectedMonkeyChanged; private MonkeySelector _monkeySelector; private WpfThunker _thunker; public WinFormsMonkeySelector() { InitializeComponent(); _monkeySelector = new MonkeySelector(); _elementHost.Child = _monkeySelector; System.Windows.Data.Binding binding = new System.Windows.Data.Binding("SelectedMonkey"); binding.Source = _monkeySelector; binding.Mode = System.Windows.Data.BindingMode.OneWay; _thunker = new WpfThunker(this);
source share