c# - 使用 MVVM 在 MainWindow 上绑定(bind) UserControl View 模型

标签 c# wpf xaml mvvm user-controls

我是 WPF 和 MVVM 的新手,我正在尝试了解 WPF 如何与 MVVM 一起工作。为此,我制作了如下示例

UserControl1.xaml

<StackPanel>
        <TextBox   Text="{Binding MyString}" />
</StackPanel>

UserControl1ViewModel.cs

class UserControl1ViewModel
{
     public string MyString { get; set; }
}

主窗口.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; }


}

问题:现在,如何将 Usercontrol 的数据上下文传递到 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; }

我试过的以上代码没有按预期工作。通过窗口传递用户控件的数据上下文的标准方法是什么?

最佳答案

您在这里对 MVVM、 View 和用户控件有一个普遍的误解。

UserControl 是一段可重用的代码,不特定于一种应用程序。也就是说,当您创建一个新的 UserControl 时,没有 UserControl1ViewModel

UserControl 是 self 维持的,您的用户控件需要的所有逻辑都在代码隐藏中。需要明确的是,这违反了 MVVM 模式。 MVVM 模式适用于 View 和 View 模型以及它们的交互方式。

View(纯 XAML,无逻辑)之间存在细微差别。 View 通常也继承自 UserControl,但 View 只适用于您现在正在开发的应用程序。您不太可能在另一个应用程序中重用它。

这是 UserControl 之间的区别。例如,日历用户控件是可重用的,所有选择和显示日历的逻辑都是它背后的控制代码的一部分,您可以在多种应用程序中使用它。

当您创建一个使用数据绑定(bind)的 UserControl 时,您需要在您的用户控件中公开依赖属性,在日期选择器用户控件上,这可能是 MinDateMaxDateSelectedDateFirstDayOfTheWeek(星期日或星期一)和/或控制格式并将所有其他属性隐藏在 中的属性UserControl 的 XAML(不通过依赖属性公开它们)。

关于c# - 使用 MVVM 在 MainWindow 上绑定(bind) UserControl View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395002/

相关文章:

c# - IntelliSense 提示缺少 Microsoft.AspNetCore 包,尽管在那里

c# - 如何获取 url 响应(内容 : xml data) from solr search engine in asp.net

c# - 是否有带缩放功能的 wpf 网络摄像头控件?

c# - 当我需要 "refresh"或 "update" View 时,为我的 ViewModel 中的每个属性调用 RaisePropertyChanged() 是不好的做法吗?

c# - WPF 删除选中项目的 ListView 边框

xaml - 每个平台在 XAML 中定义的 FontFamily

c# - (UWP) 使用 ESC 键关闭 ModalDialog

c# - 定义自定义 Powershell 对象

c# - 没有 Entity Framework 的存储库

c# - WPF 复选框绑定(bind)