C# WPF MVVM TextBox 值不会改变
我是在 WPF 中使用 MVVM 的初学者,发现似乎无法更改文本框或标签的值。这是一个例子。
在Xaml中:
Name 的原始值为“Peter”。
但是当我按下一个按钮来调用 ViewModel 中的命令并将 Name 的值更改为 “约翰”。因此,假设文本框的值也将更改为 John。然而,它并没有改变。
我在网上找了很多例子,发现没有一个实现这种功能。我从他们身上学到的是使用ListView的Command和ItemsSource。 当我使用按钮提升命令来更改视图的 ItemsSource 时,ListView 的值将会更改。当 Binding to ItemsSource 改变时,它的值会自动改变。
但是,即使它们的绑定值已经更改,我也无法更改 TextBox 或 Label 的值。
其实我对MVVM还很年轻。我想我还有很多不知道的事情。 您能给我举一个例子,说明单击按钮后我应该如何更改文本框吗?顺便说一句,我不太清楚如何为按钮创建命令。好像涉及到的代码太多了,我是从网上找的样本。有没有更简单的方法呢?
非常感谢。
I am a beginner to use MVVM in WPF and found that it seem impossible to change the value of a textbox or a label. Here is an example.
In Xaml:
The original value of Name is "Peter".
But after I press a button which invoke a command in the ViewModel and change the value of Name to be
"John". So, suppose the value of the text box will be changed to John as well. However, it doesn't change.
I have found a lot of example in the net and found that none of them implemented this kind of functions. What I have learnt from them is to use Command and ItemsSource of ListView.
The value of ListView will change when I use button to raise command to change the ItemsSource of the view. Its value will change automatically when the Binding to ItemsSource changed.
However, I cannot make the value of TextBox or Label change even the value of the bindings to them are changed already.
Actually, I am really quite young in MVVM. I think I still have so much that I don't know.
Could you give me an example of how exactly I should do to make change to textbox after a button click? By the way, I am not quite sure how to make command for button. It seem to involve so much codes that I found in the sample from the net. Is there any simplier way?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 ViewModel 需要实现 INotifyPropertyChanged 。
文档请参阅此处
Your ViewModel needs to implement
INotifyPropertyChanged
.Documentation see here
您的视图模型应实现 INotifyPropertyChanged 以便 WPF 知道您更改了财产的价值。
这是一个例子
Your view model should implement INotifyPropertyChanged so that WPF knows that you've altered a value of a property.
Here is an example from