数据改变后更新UI(SetProperty/RaiseCanExecuteChanged)

View代码

 1  <StackPanel>
 2             <TextBlock Text="方法一"></TextBlock>
 3             <TextBox Text="{Binding Value1}"></TextBox>
 4             <TextBlock Text="方法二"></TextBlock>
 5             <TextBox Text="{Binding Value2}"></TextBox>
 6             <TextBlock Text="方法三"></TextBlock>
 7             <TextBox Text="{Binding Value3}"></TextBox>
 8             <TextBlock Text="方法四"></TextBlock>
 9             <TextBox Text="{Binding Value4}"></TextBox>
10             <TextBlock Text="方法五"></TextBlock>
11             <TextBox Text="{Binding Value5}"></TextBox>
12             <Button Content="方法一" Command="{Binding AddCommand1}"></Button>
13             <Button Content="方法二" Command="{Binding AddCommand2}"></Button>
14             <Button Content="方法三" Command="{Binding AddCommand3}"></Button>
15             <Button Content="方法四" Command="{Binding AddCommand4}"></Button>
16             <Button Content="方法五" Command="{Binding AddCommand5}"></Button>
17         </StackPanel>

ViewModel代码

  1  public class MainWindowViewModel : BindableBase
  2     {
  3         private string _title = "Prism命令学习";
  4         public string Title
  5         {
  6             get
  7             {
  8                 return _title;
  9             }
 10             set
 11             {
 12                 SetProperty(ref _title, value);
 13             }
 14         }
 15 
 16        
 17         public MainWindowViewModel()
 18         {
 19             //方法一
 20             AddCommand1 = new DelegateCommand(Add1);
 21            
 22             //方法二
 23             AddCommand2 = new DelegateCommand(Add2);
 24             //方法三
 25             AddCommand3 = new DelegateCommand(Add3);
 26             //方法四
 27             AddCommand4 = new DelegateCommand(Add4);
 28             //方法五:布尔类型,可以使控件不能使用变灰
 29             AddCommand5 = new DelegateCommand(Add5).ObservesCanExecute(() => Value5);
 30 
 31         }
 32         private Random random = new Random();
 33         //方法一
 34         private string _value1 = "Prism命令学习";
 35         public string Value1
 36         {
 37             get
 38             {
 39                 return _value1;
 40             }
 41             set
 42             {
 43                 SetProperty(ref _value1, value);
 44             }
 45         }
 46         public DelegateCommand? AddCommand1
 47         {
 48             set; get;
 49         }       
 50         private void Add1()
 51         {
 52             //写入需要执行的代码;
 53 
 54             Value1 =random.Next().ToString();
 55             MessageBox.Show(Value1);
 56         }
 57 
 58 
 59 
 60         //方法二
 61         private string _value2 = "Prism命令学习";
 62         public string Value2
 63         {
 64             get
 65             {
 66                 return _value2;
 67             }
 68             set
 69             {
 70                 SetProperty(ref _value2, value);
 71                 AddCommand2.RaiseCanExecuteChanged();
 72             }
 73         }
 74         public DelegateCommand? AddCommand2
 75         {
 76             set; get;
 77         }
 78 
 79       
 80         private void Add2()
 81         {
 82             //写入需要执行的代码;
 83 
 84             Value2= random.Next().ToString();
 85             MessageBox.Show(Value2);
 86         }
 87 
 88 
 89         //方法三
 90         private string _value3 = "Prism命令学习";
 91         public string Value3
 92         {
 93             get
 94             {
 95                 return _value3;
 96             }
 97             set
 98             {
 99                 _value3 = value;
100                 RaisePropertyChanged(nameof(Value3));
101                 //也可以这样
102                 RaisePropertyChanged();
103 
104             }
105         }
106         public DelegateCommand? AddCommand3
107         {
108             set; get;
109         }
110 
111 
112         private void Add3()
113         {
114             //写入需要执行的代码;
115 
116             Value3 = random.Next().ToString();
117             MessageBox.Show(Value3);
118         }
119         //方法四
120         private string _value4 = "Prism命令学习";
121         public string Value4
122         {
123             get
124             {
125                 return _value4;
126             }
127             set
128             {
129                 _value4 = value;
130               OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(nameof(Value4)));
131 
132             }
133         }
134         public DelegateCommand? AddCommand4
135         {
136             set; get;
137         }
138 
139 
140         private void Add4()
141         {
142             //写入需要执行的代码;
143 
144             Value4 = random.Next().ToString();
145             MessageBox.Show(Value4);
146         }
147         //方法五
148         private bool _value5= false;
149         public bool Value5
150         {
151             get
152             {
153                 return _value5;
154             }
155             set
156             {
157                 _value5 = value;
158                 SetProperty(ref _value5, value);
159               
160 
161             }
162         }
163         public DelegateCommand? AddCommand5
164         {
165             set; get;
166         }
167 
168 
169         private void Add5()
170         {
171             //写入需要执行的代码;
172 
173             Value5=true;
174             MessageBox.Show(Value5.ToString());
175         }
176     }

 

posted @ 2023-03-27 23:55  Windows窗  阅读(67)  评论(0编辑  收藏  举报