如何将WPF、MVVM和绑定与WinForm UserControl成功集成?
我在WPF窗口中有一个WinForm UserControl,WPF代码使用MVVM模式 成功地将WinForm控件集成到MVVM模式中的最佳方法是什么? 我可以从WPF方面使用某种形式的绑定吗 假设我想处理来自WF控件的一些事件,有没有一种方法可以完全进入MVVM如何将WPF、MVVM和绑定与WinForm UserControl成功集成?,wpf,winforms,mvvm,user-controls,Wpf,Winforms,Mvvm,User Controls,我在WPF窗口中有一个WinForm UserControl,WPF代码使用MVVM模式 成功地将WinForm控件集成到MVVM模式中的最佳方法是什么? 我可以从WPF方面使用某种形式的绑定吗 假设我想处理来自WF控件的一些事件,有没有一种方法可以完全进入MVVM 谢谢。我个人会创建一个包装Windows窗体控件的WPF UserControl来处理这个问题。这将允许您将所有需要的代码封装到WPF控件中,然后以纯MVVM的方式使用它 直接使用Windows窗体控件将很难保持“纯”MVVM,因为
谢谢。我个人会创建一个包装Windows窗体控件的WPF UserControl来处理这个问题。这将允许您将所有需要的代码封装到WPF控件中,然后以纯MVVM的方式使用它
直接使用Windows窗体控件将很难保持“纯”MVVM,因为Windows窗体控件通常需要不同的绑定模型,并且通常需要直接的事件处理。您可以看看。它展示了一种将Windows窗体与MVVM结合使用的可能方法。请注意,这并不能真正回答问题(我应该读得更好)。如果您对在WinForms应用程序中使用WPF控件感兴趣,这里有一种方法。我的设想是: 1) 有一个WinForms控件,它在我的应用程序中的许多地方都使用。 2) 希望开发一个使用MVVM模式的WPF实现。 3) 希望将该控件编写为一个正确的WPF控件,并带有依赖项属性,以便在我的应用程序最终全部为WPF时可以正确使用。 4) 希望保持相同的WinForms控件和API以不破坏我的应用程序中的现有客户端代码 除了让WinForms控件在WPF控件的属性更改时引发事件外,大多数事情都很简单。我想使用绑定,但由于绑定的源必须是DependencyObject,而System.Windows.Forms.UserControl不是,所以我必须创建一个简单的嵌套类。我编写我的WPF控件就像我将它集成到WPF应用程序中一样,只是做了一些额外的工作来让我的WinForms包装器工作 以下是我的WPF控件的代码:
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);
// Note: No code is shown for binding the SelectedMonkey dependency property
// with the ViewModel's SelectedMonkey property. This is done by creating
// a Binding object with a source of ViewModel (Path = SelectedMonkey) and
// target of the SelectedMonkey dependency property. In my case, my
// ViewModel was a resource declared in XAML and accessed using the
// FindResource method.
}
public IMonkey SelectedMonkey
{
get { return (IMonkey)GetValue(SelectedMonkeyProperty); }
set { SetValue(SelectedMonkeyProperty, value); }
}
}
以下是我的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);
// Note: The second parameter here is arbitray since we do not actually
// use it in the thunker. It cannot be null though. We could declare
// a DP in the thunker and bind to that, but that isn't buying us anything.
System.Windows.Data.BindingOperations.SetBinding(
_thunker,
MonkeySelector.SelectedMonkeyProperty,
binding);
}
protected virtual void OnSelectedMonkeyChanged()
{
if (SelectedMonkeyChanged != null)
SelectedMonkeyChanged(this, EventArgs.Empty);
}
public IMonkey SelectedMonkey
{
get { return _monkeySelector.SelectedMonkey; }
set { _monkeySelector.SelectedMonkey = value; }
}
private class WpfThunker : System.Windows.DependencyObject
{
private WinFormsMonkeySelector _parent;
public WpfThunker(WinFormsMonkeySelector parent)
{
_parent = parent;
}
protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
// Only need to check the property here if we are binding to multiple
// properties.
if (e.Property == MonkeySelector.SelectedMonkeyProperty)
_parent.OnSelectedMonkeyChanged();
}
}
}
因此,如果有一个封装WinForms控件的WPF usercontrol,我可以从WPF usercontrol提供WPF绑定,但在内部它将处理WinForms控件?如果是这样的话,那么它可能就是我想要的方式。@Stecy:Yep-在内部,它将完成从WPF DependencyProperties到设置控件值、接收事件等的映射。但是,从WPF方面来看,这有一个优点,就是看起来与普通WPF控件完全一样。从长远来看,这确实是最好的办法,我想。