Binding UserControl View model on MainWindow with MVVM

女生的网名这么多〃 提交于 2020-01-10 04:27:05

问题


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 UserControls XAML (by not exposing them via Dependency Properties).



来源:https://stackoverflow.com/questions/35395002/binding-usercontrol-view-model-on-mainwindow-with-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!