-1

I have a beginer question. Maybe i'm confused and don't fully figure the binding concept out.

I try to use binding, but i do not see any update on a textbox when i change the source value.

XLAM file :

<TextBox IsReadOnlyCaretVisible="True" AutomationProperties.Name="keyIdTextBox" Text="{Binding Path=Name, Mode=oneWay}"/>

...

<Button Content="Open" AutomationProperties.Name="openCloseButton" Click="OpenClose_Click"/>

C# file :

public class Person
{
    private string nameValue;
    public string Name
    {
        get { return nameValue; }
        set { nameValue = value; }
    }
}


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private Person person = new Person { Name = "Bob" };

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


    private void OpenClose_Click(object sender, RoutedEventArgs e)
    {
        person.Name = "Lenny";
    }

At start, the Textbox is well updated and display "Bob", but when i click on the button to update the value of Name, The textbox does not reflect the value "Lenny". There is no change.

Thank you for you help.

Yaegaki2
  • 9
  • 4

1 Answers1

0

Option 1: INotifyPropertyChanged

The Person class should implement the INotifyPropertyChanged interface to inform targets when a property changes.

This is the modified working example.

public class Person : INotifyPropertyChanged
{
    private string nameValue;

    public string Name
    {
        get { return nameValue; }
        set
        {
            nameValue = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public partial class MainWindow : Window
{
    private Person person = new Person { Name = "Bob" };

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

    private void OpenClose_Click(object sender, RoutedEventArgs e)
    {
        person.Name = "Lenny";
    }
}

Option 2: DependencyProperty

Define Name property as a DependencyProperty. The infrastructure will handle the changes.

Check out the example below.

public class Person : DependencyObject
{
    public static readonly DependencyProperty
        NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Person));

    public string Name
    {
        get => (string)GetValue(NameProperty);
        set => SetValue(NameProperty, value);
    }
}

public partial class MainWindow : Window
{
    private Person person = new Person { Name = "Bob" };

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

    private void OpenClose_Click(object sender, RoutedEventArgs e)
    {
        person.Name = "Lenny";
    }
}
Hadi Fooladi Talari
  • 1,180
  • 1
  • 13
  • 36