在主窗口绑定用户控件视图模型与MVVM [英] Binding UserControl View model on MainWindow with MVVM
问题描述
我是新手,WPF和MVVM,我努力学习WPF如何与MVVM工作。对于这一点,我做了一个样本如下:
I am newbie to WPF and MVVM and I am trying to learn how WPF working with MVVM. For that, I have made a sample as follows
<StackPanel>
<TextBox Text="{Binding MyString}" />
</StackPanel>
UserControl1ViewModel.cs
UserControl1ViewModel.cs
class UserControl1ViewModel
{
public string MyString { get; set; }
}
MainWindow.xaml
MainWindow.xaml
<StackPanel>
<local:UserControl1 DataContext="{Binding UC1Property}"/> //tried binding the Usercontrol1VM obj on MainWindowVM
<Button Command="{Binding ShowMeOne}" Height="30" Content="ShowOne"/>
<Button Command="{Binding ShowMeAnother}" Height="30" Content="ShowAnother" />
</StackPanel>
MainWindow.xaml.cs
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
MainWindowViewModel.cs
MainWindowViewModel.cs
class MainWindowViewModel
{
public MainWindowViewModel()
{
ShowMeOne = new RelayCommand(Prompt_ShowMeOne);
ShowMeAnother = new RelayCommand(Prompt_ShowMeAnother);
UC1Property.MyString = "Initial";
}
private void Prompt_ShowMeAnother(object obj)
{
global::System.Windows.MessageBox.Show("Another Should be shown");
UC1Property.MyString = "Last Clicked: Another";
}
private void Prompt_ShowMeOne(object obj)
{
global::System.Windows.MessageBox.Show("One Should be shown");
UC1Property.MyString = "Last Clicked: One";
}
public ICommand ShowMeOne { get; set; }
public ICommand ShowMeAnother { get; set; }
//UserControl1 View Model for MainWindow
public UserControl1ViewModel UC1Property { get; set; }
}
问题:?的现在,我可以怎样通过用户控件的DataContext的到主窗口
Problem: Now, how can I pass the Datacontext of the Usercontrol into the MainWindow?
-----------------------------In MainWindow.xaml----------------------
<local:UserControl1 DataContext="{Binding UC1Property}"/> //tried binding the Usercontrol1VM obj on MainWindowVM
-----------------------------In MainWindowViewModel.cs---------------
//UserControl1 View Model for MainWindow
public UserControl1ViewModel UC1Property { get; set; }
以上是我试过无法按预期工作的代码。什么是经过用户控件的DataContext的在窗口的标准方式?
Above code which I tried is not working as expected. What is the standard way of passing the datacontext of usercontrol over the window?
推荐答案
您有一个普遍的误解在这里对MVVM,视图和用户控件。
You got a general misunderstanding here about MVVM, Views and UserControls.
A 用户控件
是一个可重用的代码块,这不是针对一个类型的应用程序。话虽这么说,没有 UserControl1ViewModel
,当你创建一个新的用户控件
。
A UserControl
is a reusable piece of code, that is not specific to one kind of application. That being said, there is no UserControl1ViewModel
, when you create a new UserControl
.
A 用户控件
是自我维持的,所有的逻辑用户控制的需要去那里在后面的代码。要清楚,这是不会违反MVVM格局。 MVVM模式适用于视图和的ViewModels以及它们如何相互作用。
A UserControl
is self-sustained and all the logic your user control needs goes in there in code behind. To make it clear, this is not a violation of MVVM pattern. MVVM pattern applies to Views and ViewModels and how they interact.
有一个查看
(纯XAML,没有逻辑)之间的细微差别。意见还经常从用户控件
继承,但查看
仅在应用程序现在你正在开发好。你是不太可能能够在其他应用程序重用这一点。
There is a subtle difference between a View
(pure XAML, no logic). Views also often inherit from UserControl
, but a View
is only good in the application you are developing right now. You are very unlikely to be able to reuse this in another application.
这是一个区别,之间的用户控件
。例如,一个日历的用户控件是可重复使用,所有的逻辑选择和显示日历是它的控制代码部分的后面,您可以在许多种应用程序中使用它。
That's a difference, between the UserControl
. For example, a calendar user control is reusable and all the logic to choose and display the calendar is part of it's control code behind and you can use it in many kind of applications.
当你创建一个用户控件
它使用数据绑定,你需要在你的用户控件暴露依赖属性,在一个日期选取器的用户控制,这可能是为MinDate
, MAXDATE
, SelectedDate
, FirstDayOfTheWeek
(周日或周一)和/或性能控制的格式和隐藏在你的用户控件$ C所有其他属性$ C>取值XAML(由通过依赖属性不暴露他们)。
When you create a UserControl
which uses data-bindings, you need to expose dependency properties in your user control, on an date-picker user control this may be MinDate
, MaxDate
, SelectedDate
, FirstDayOfTheWeek
(Sunday or Monday) and/or properties that control the formatting and hide all other properties inside your UserControl
s XAML (by not exposing them via Dependency Properties).
这篇关于在主窗口绑定用户控件视图模型与MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!