C# WPF MVVM TextBox 值不会改变

发布于 2024-11-29 04:29:41 字数 538 浏览 1 评论 0原文

我是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-06 04:29:41

您的 ViewModel 需要实现 INotifyPropertyChanged 。
文档请参阅此处

public class Bar : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private string foo;
  public string Foo 
  {
    get { return this.foo; }
    set 
    { 
      if(value==this.foo) 
        return;
      this.foo = value;
      this.OnPropertyChanged("Foo");
    }
  }
  private void OnPropertyChanged(string propertyName)
  {
    if(this.PropertyChanged!=null)
      this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
  }  
}

Your ViewModel needs to implement INotifyPropertyChanged .
Documentation see here

public class Bar : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private string foo;
  public string Foo 
  {
    get { return this.foo; }
    set 
    { 
      if(value==this.foo) 
        return;
      this.foo = value;
      this.OnPropertyChanged("Foo");
    }
  }
  private void OnPropertyChanged(string propertyName)
  {
    if(this.PropertyChanged!=null)
      this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
  }  
}
烙印 2024-12-06 04:29:41

您的视图模型应实现 INotifyPropertyChanged 以便 WPF 知道您更改了财产的价值。

这是一个例子

// This is a simple customer class that 
// implements the IPropertyChange interface.
public class DemoCustomer  : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private string customerNameValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        var listeners = PropertyChanged;
        if (listeners  != null) 
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }
}

Your view model should implement INotifyPropertyChanged so that WPF knows that you've altered a value of a property.

Here is an example from

// This is a simple customer class that 
// implements the IPropertyChange interface.
public class DemoCustomer  : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private string customerNameValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        var listeners = PropertyChanged;
        if (listeners  != null) 
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文