WPF基础入门
Class7-MVVN框架
使用框架可以省掉如Class6中的ViewModelBase.cs
的OnPropertyChanged
,亦方便命令传参
1、NuGet安装CommunityToolkit.Mvvm
(原Mircrosoft.Toolkit.Mvvm)也可以安装MVVMLight等其他集成库
2、显示页面:
<Grid><StackPanel><TextBox x:Name="input" Text="{Binding Name}"></TextBox><TextBox Text="{Binding Title}"></TextBox><!--CommandParameter传参到命令中--><ButtonCommand="{Binding ShowCommand}"CommandParameter="{Binding ElementName=input, Path=Text}">Button</Button></StackPanel></Grid>
2、model文件:
class model_csdn : ObservableObject{public model_csdn(){Name = "Ini_name";Title = "点击后变成Name内容";ShowCommand = new RelayCommand<string>(show);}//public MyCommamd ShowCommand { get; set; }public RelayCommand<string> ShowCommand { get; set; }public string name;public string title;public string Name{get { return name; }set{name = value;OnPropertyChanged();}}public string Title{get { return title; }set{title = value;OnPropertyChanged();}}public void show(string value){Name = "change name";Title = value;MessageBox.Show("value");}}
3、显示效果: