[英]Bind User Control to view model
I am still getting my head around MvvM so please bear with me. 我仍然对MvvM有所了解,所以请多多包涵。
I have a simple User Control called DatePickerUC 我有一个名为DatePickerUC的简单用户控件
partial class DatePickerUC
{
...........
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.Button button1;
}
I now need to load this user control into my ViewModel and then reference it from my XAML code so that the user control containing the 2 date pickers and button load at runtime. 现在,我需要将此用户控件加载到ViewModel中,然后从XAML代码中引用它,以便该用户控件包含2个日期选择器和按钮在运行时加载。
But not sure how to go about it 但不确定如何去做
Thanks 谢谢
In general, you really shouldn't be doing this. 通常,您实际上不应该这样做。 Especially with WinForms controls.
尤其是 WinForms控件。
However, Content
is a dependency property, so it can be bound. 但是,
Content
是依赖项属性,因此可以进行绑定。 You can write this: 您可以这样写:
<ContentPresenter Content="{Binding CurrentView}" />
If CurrentView
is a property that returns that user control, it will work fine. 如果
CurrentView
是返回该用户控件的属性,则它将正常工作。 Its complicated by the fact that you have WinForms controls, you'll need a WPF WindowsFormsHost wrapper around those controls for it to work. 由于具有WinForms控件,因此使其变得复杂,您需要在这些控件周围使用WPF WindowsFormsHost包装器才能使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.