WPF框架mvvmlight

1.引用NuGet程序包:MvvMLightLibs:

image

 2.实现通知功能的使用方式:

 RaisePropertyChanged();

3.使用Command功能的使用方式:
ShowCommand = new RelayCommand(Show);  //不带参数
 ShowCommand1 = new RelayCommand<string>(Show1); //带参数

public class MainViewModel:ViewModelBase
{
    public MainViewModel() 
    {
        Name = "Hello";
        ShowCommand = new RelayCommand(Show);
        ShowCommand1 = new RelayCommand<string>(Show1);
    }

    private string name;

    public string Name
    {
        get { return name; }
        set
        { 
            name = value;
            RaisePropertyChanged();
        }
       
    }

    public RelayCommand ShowCommand { get; set; }


    public RelayCommand<string> ShowCommand1 { get; set; }


    public void Show()
    {
        Name = "点击了按钮";
        MessageBox.Show("点击按钮");
    }

    public void Show1(string str)
    {
      
        MessageBox.Show(str);
    }

UI代码:

 <Grid>
     <StackPanel>
         <TextBox Text="{Binding Name}"/>
         <Button Command="{Binding ShowCommand}" Height="30"/>
         <TextBox x:Name="txtInput" Margin="10"/>
         <Button Command="{Binding ShowCommand1}"
                 CommandParameter="{Binding ElementName=txtInput,Path=Text}"
                 Height="30"
                 />
     </StackPanel>
 </Grid>

设置上下文:

 public partial class MainWindow : Window
 {
     public MainWindow()
     {
         InitializeComponent();
         DataContext = new MainViewModel();
     }
 }

3.消息功能:可在任意的地方注册消息,然后在其他地方发消息:

image

 发消息:

image

 

posted @ 2026-05-19 19:59  你好呀嗯嗯  阅读(9)  评论(0)    收藏  举报