简体   繁体   中英

DataBinding to a TextBox not working

I am having a difficult time getting my WPF to properly use Databinding. In the XAML I have a the following:

....
<TextBox Name="txt_FirstName" Text="{Binding Path=currentApplication.FirstName, UpdateSourceTrigger=PropertyChanged}" />
....

I have in the following CS code:

namespace WPF1
{
  public partial class MainWindow : Window
  {
    personalApp currentApplication = new personalApp ();

    public MainWindow()
    {
      InitializeComponent();
    }

  }
}

That references the following two classes:

class personalApp : INotifyPropertyChanged
{
  private Person person = new Person();

  public string FirstName
  {
    get { return person.FirstName; }
    set
    {
      person.FirstName = value;
      this.OnPropertyChanged("FirstName");
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  void OnPropertyChanged(string propName)
  {
    if (this.PropertyChanged != null)
      this.PropertyChanged(
      this, new PropertyChangedEventArgs(propName));
   }

}

class Person
{
  private string firstName = "";

  get { return firstName; }
  set { FirstName = value; }
}

I pause it in the code and step through to check, but when I update the txt_FirstName in the application, it never seems to set the firstName Object.

Where am I going wrong?

You need to update your XAML binding, and set the DataContext of the Window using the TextBox .

namespace WPF1
{
  public partial class MainWindow : Window
  {
    personalApp currentApplication = new personalApp ();

    public MainWindow()
    {
      InitializeComponent();
      this.DataContext = currentApplication;
    }
  }
}

Updating the XAML:

<TextBox Name="txt_FirstName" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" />

I have corrected the code.

For text box:

<TextBox Name="txt_FirstName" Height="30" Background="Beige"
             Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />

C# Code

namespace Wpf1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new personalApp();
    }
}

internal class personalApp : INotifyPropertyChanged
{
    private Person person = new Person();

    public string FirstName
    {
        get { return person.FirstName; }
        set
        {
            person.FirstName = value;
            this.OnPropertyChanged("FirstName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(
            this, new PropertyChangedEventArgs(propName));
    }
}

internal class Person
{
    public string FirstName { get; set; }
}
}
public MainWindow()
{
  DataContext = this;
  InitializeComponent();
}

or if you don't want to assign data context to yourself (window), as you might have other datacontext coming into the window, you can add this in xaml: give your window a name:

<Window .... x:Name="this"...

then

<TextBox Name="txt_FirstName" Text="{Binding ElementName=this,
                                     Path=currentApplication.FirstName/>

What you mean by "update the txt_FirstName in the application" ?
If you set directly the value of the textbox then you should try to set the value of currentApplication instead of the textbox value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM