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传参到命令中-->
<Button
Command="{Binding ShowCommand}"
CommandParameter="{Binding ElementName=input, Path=Text}">Button</Button>
</StackPanel>
</Grid>
2、model文件:文章来源:https://www.toymoban.com/news/detail-682424.html
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、显示效果:
文章来源地址https://www.toymoban.com/news/detail-682424.html
到了这里,关于WPF基础入门-Class7-WPF-MVVN框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!