我是MVVM和WPF的新手,但我知道MVVM中发生了什么。我在mainwindow中的用户控件之间切换时遇到问题。在我的应用程序中,我有:MainWindow.xaml,带日志和2个链接:全部显示并创建新的。当然我有ViewModel。我还有2个UserControls:ShowAll和Create with ViewModels及其中的所有逻辑(添加数据等)。单击链接“新建”或单击“全部显示”时如何显示创建表单?
在windowForms中,我只是隐藏UC,但是这里没有任何代码:)
我的MainWindow.xaml:
<Window x:Class="Test.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Grid>
<StackPanel>
<TextBox Text="{Binding Name}"/>
<Button Content="Change" Command="{Binding ChangeCommand}"/>
</StackPanel>
</Grid>
</Window>
我的MainWindowViewModel:
class MainWindowViewModel : BaseViewModel
{
private Person _person;
private BaseCommand _changeCommand;
public MainWindowViewModel()
{
_person = new Person();
}
public string Name
{
get
{
return _person.Name;
}
set
{
if (_person.Name != value)
_person.Name = value;
OnPropertyChanged(() => Name);
}
}
public ICommand ChangeCommand
{
get
{
if (_changeCommand == null)
_changeCommand = new BaseCommand(() => change());
return _changeCommand;
}
}
private void change()
{
_person = new Person();
Name = _person.Imie;
}
}
在创建和ShowAll中没有代码。在xaml中只有一个标签,VM为空。只是为了测试。
感谢帮助!
您可以使用ContentControl
来显示DataTemplate
基于绑定到的ViewModel类型的特定对象ContentControl
。
http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/
绑定到ShowAll按钮的命令可以简单地更改主ViewModel上的属性,该属性就是绑定到内容控件的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句