C# 将MainWindow上的UserControl视图模型与MVVM绑定
我是WPF和MVVM的新手,我正在尝试学习WPF如何与MVVM一起工作。为此,我制作了如下样本 UserControl1.xaml main window.xaml MainWindowViewModel.cs 问题:现在,如何将Usercontrol的Datacontext传递到主窗口C# 将MainWindow上的UserControl视图模型与MVVM绑定,c#,wpf,xaml,mvvm,user-controls,C#,Wpf,Xaml,Mvvm,User Controls,我是WPF和MVVM的新手,我正在尝试学习WPF如何与MVVM一起工作。为此,我制作了如下样本 UserControl1.xaml main window.xaml MainWindowViewModel.cs 问题:现在,如何将Usercontrol的Datacontext传递到主窗口 -----------------------------In MainWindow.xaml---------------------- <local:UserControl1 DataContext=
-----------------------------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; }
-----------------------------在MainWindow.xaml中----------------------
//已尝试在MainWindowVM上绑定Usercontrol1VM对象
-----------------------------在MainWindowViewModel.cs中---------------
//主窗口的UserControl1视图模型
public UserControl1ViewModel UC1Property{get;set;}
我尝试的上述代码并没有按预期工作。在窗口上传递usercontrol的datacontext的标准方式是什么?您对MVVM、视图和usercontrol有一个普遍的误解
UserControl
是一段可重用的代码,它不是特定于一种应用程序的。也就是说,当您创建新的UserControl
时,没有UserControl1ViewModel
UserControl
是自我维持的,用户控件需要的所有逻辑都在代码背后。明确地说,这不是违反MVVM模式。MVVM模式适用于视图和视图模型及其交互方式
在视图
(纯XAML,无逻辑)之间存在细微差别。视图通常也继承自UserControl
,但是视图
只适用于您正在开发的应用程序。您不太可能在另一个应用程序中重用它
这就是UserControl
之间的区别。例如,日历用户控件是可重用的,选择和显示日历的所有逻辑都是其控制代码的一部分,您可以在许多应用程序中使用它
创建使用数据绑定的
UserControl
时,需要在用户控件中公开依赖项属性,在日期选择器用户控件上,这可能是MinDate
,MaxDate
,SelectedDate
,周的第一天
(星期日或星期一)和/或属性,这些属性控制格式化并隐藏UserControl
s XAML中的所有其他属性(通过不通过依赖属性公开它们) 我想你应该重新考虑一下。如果你的主视图是那样绑定到UserControl上的,你为什么要使用它?@Stefan。这只是一个更好理解的示例。我希望这是应该如何实施的。如果我错了,请纠正我。@Gopichandar:请看下面我的答案。自定义用户控件没有自己的ViewModel。它们应该是可重用的,并且ViewModel对于您正在开发的应用程序来说是非常特定的。MVVM模式用于应用程序开发,而不是用户controlsAw!真有趣。我对MVVM及其实现有不同的想法。谢谢你纠正我。
class UserControl1ViewModel
{
public string MyString { get; set; }
}
<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>
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
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; }
}
-----------------------------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; }