WPF:简单的 TextBox 数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1725554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:11:46  来源:igfitidea点击:

WPF: simple TextBox data binding

wpfdata-bindingtextbox

提问by Warpin

I have this class:

我有这门课:

public partial class Window1 : Window
{
    public String Name2;

    public Window1()
    {
        InitializeComponent();
        Name2 = new String('a', 5);
        myGrid.DataContext = this;
    }

    // ...
}

And I want to display the string Name2in the textbox.

我想Name2在文本框中显示字符串。

<Grid Name="myGrid" Height="437.274">
  <TextBox Text="{Binding Path=Name2}"/>
</Grid>

But the string isn't displayed. Also, if the string Name2is updated periodically using a TimerCallback, do I need to do anything to make sure the textbox is updated when the data changes?

但是不显示字符串。此外,如果Name2使用 a 定期更新字符串TimerCallback,我是否需要做任何事情来确保在数据更改时更新文本框?

回答by itowlson

Name2 is a field. WPF binds only to properties. Change it to:

Name2 是一个字段。WPF 仅绑定到属性。将其更改为:

public string Name2 { get; set; }

Be warned that with this minimal implementation, your TextBox won't respond to programmatic changes to Name2. So for your timer update scenario, you'll need to implement INotifyPropertyChanged:

请注意,使用此最小实现,您的 TextBox 将不会响应 Name2 的编程更改。因此,对于您的计时器更新方案,您需要实现 INotifyPropertyChanged:

partial class Window1 : Window, INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string propertyName)
  {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }

  private string _name2;

  public string Name2
  {
    get { return _name2; }
    set
    {
      if (value != _name2)
      {
         _name2 = value;
         OnPropertyChanged("Name2");
      }
    }
  }
}

You should consider moving this to a separate data object rather than on your Window class.

您应该考虑将其移动到单独的数据对象而不是您的 Window 类。

回答by Darien Ford

Your Window is not implementing the necessary data binding notifications that the grid requires to use it as a data source, namely the INotifyPropertyChangedinterface.

您的 Window 没有实现网格将其用作数据源所需的必要数据绑定通知,即INotifyPropertyChanged接口。

Your "Name2" string needs also to be a property and not a public variable, as data binding is for use with properties.

您的“Name2”字符串也需要是一个属性而不是一个公共变量,因为数据绑定是用于属性的。

Implementing the necessary interfaces for using an object as a data source can be found here.

可以在此处找到实现将对象用作数据源所需的接口。

回答by Stefan Cantacuz

Just for future needs.

只是为了将来的需要。

In Visual Studio 2013 with .NET Framework 4.5, for a window property, try adding ElementName=windowto make it work.

在带有 .NET Framework 4.5 的 Visual Studio 2013 中,对于窗口属性,尝试添加ElementName=window以使其工作。

<Grid Name="myGrid" Height="437.274">
  <TextBox Text="{Binding Path=Name2, ElementName=window}"/>
</Grid>