0

I have made a test application using Winforms and C#. 2 textbox's Text properties are bound to the same custome property. custom property implements an event PropertyChangedEventHandler as is described in this post.

the issue is that then I modify one of the textboxes Taxt value, the other gets modified too. but thats not true if I modify the property they are bound to.

Here is the code:

public partial class Form1 : Form
{
    private string _text;

    public string TextProperty
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            InvokePropertyChanged();
        }
    }

    #region implement event

    private event PropertyChangedEventHandler PropertyChanged;

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

    #endregion implement event

    public Form1()
    {
        InitializeComponent();
        textBox1.DataBindings.Add("Text", this, "TextProperty", false, DataSourceUpdateMode.OnPropertyChanged);
        textBox2.DataBindings.Add("Text", this, "TextProperty", false, DataSourceUpdateMode.OnPropertyChanged);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TextProperty = "text is not changin in textBox Controls";
    }
}

can somebody help to understand the issue. here why TextProperty = "text is not changin in textBox Controls"; doesn't update the controls?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
r9guy
  • 65
  • 7
  • I have compared my code with @Stecya 's code and the difference is that it implements properties as members of a class derived from INotifyPropertyChange. but how to do if the property is a membeer of a Form1 class derived from Form? – r9guy Nov 27 '19 at 05:03
  • Your form still needs to implement `INotifyPropertyChanged`, which includes being declared as doing so. See marked duplicate. That said, it would be _much_ better if you did not mix your UI code with your data code. It would better to use a separate data class, as in the post you've referenced. – Peter Duniho Nov 27 '19 at 05:24

0 Answers0