问题
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
UserControl1.xaml
<StackPanel>
<TextBox Text="{Binding MyString}" />
</StackPanel>
UserControl1ViewModel.cs
class UserControl1ViewModel
{
public string MyString { get; set; }
}
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
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
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; }
}
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; }
Above code which I tried is not working as expected. What is the standard way of passing the datacontext of usercontrol over the window?
回答1:
You got a general misunderstanding here about MVVM, Views and UserControls.
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 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.
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.
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).
来源:https://stackoverflow.com/questions/35395002/binding-usercontrol-view-model-on-mainwindow-with-mvvm