WPF基础入门-Class7-WPF-MVVN框架

WPF基础入门

Class7-MVVN框架

使用框架可以省掉如Class6中的ViewModelBase.csOnPropertyChanged,亦方便命令传参
1、NuGet安装CommunityToolkit.Mvvm(原Mircrosoft.Toolkit.Mvvm)也可以安装MVVMLight等其他集成库

2、显示页面:

    <Grid>
        <StackPanel>
            <TextBox x:Name="input" Text="{Binding Name}">TextBox>
            <TextBox Text="{Binding Title}">TextBox>
            
            <Button
                Command="{Binding ShowCommand}"
                CommandParameter="{Binding ElementName=input, Path=Text}">ButtonButton>
        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、显示效果:
WPF基础入门-Class7-WPF-MVVN框架_第1张图片
WPF基础入门-Class7-WPF-MVVN框架_第2张图片

你可能感兴趣的:(WPF基础入门,wpf)