Problem with data binding in C #

I am trying to understand how data binding in .net works. I checked this article and I came up with this code:

public partial class Form1 : Form//, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler MyTextChanged;

    [System.ComponentModel.Bindable(true)]
    public string MyText
    {
        get { return textBox1.Text; }
        set 
        {
            textBox1.Text = value;
            if (MyTextChanged != null)
                MyTextChanged(this, new PropertyChangedEventArgs("MyText"));
        }
    }

    MyClass myClass { get; set; }

    public Form1()
    {
        InitializeComponent();
        myClass = new MyClass();
        Binding binding = new Binding("MyText", myClass, "Dic");
        binding.Parse += new ConvertEventHandler(binding_Parse);
        binding.Format += new ConvertEventHandler(binding_Format);
        DataBindings.Add(binding);
        myClass.AddStuff("uno", "UNO");
    }

    void OnMyTextChanged(PropertyChangedEventArgs e)
    {
        if (MyTextChanged != null) MyTextChanged(this, e);
    }

    void binding_Format(object sender, ConvertEventArgs e)
    {
        if (e.Value is Dictionary<string, string>)
        {
            Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
            e.Value = source.Count.ToString();
        }
    }

    void binding_Parse(object sender, ConvertEventArgs e)
    {
        MessageBox.Show(e.DesiredType.ToString());
    }

    private void changemyClassButton_Click(object sender, EventArgs e)
    {
        myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
    }

    private void changeMyTextButton_Click(object sender, EventArgs e)
    {
        MyText = "1234";
    }
}

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Dictionary<string, string> Dic { get; set; }

    public MyClass()
    {
        Dic = new Dictionary<string, string>();
    }

    public void AddStuff(string key, string value)
    {
        Dic.Add(key, value);
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
    }
}

MyText myClass. , binding_Parse . , textBox1.Text myClass , , ; , . , . myClass, MyText. , INotifyPropertyChanged, binding_Parse ( , , changeMyTextButton_Click). - ?

Edit: : string MyText, - , , TextBox Text - . , , .

+3
2

, - . MyTextChanged, Binding MyText, Binding DataSourceUpdateMode OnPropertyChanged. , :). :

public partial class Form1 : Form
{
    public event EventHandler MyTextChanged;

    [Bindable(true)]
    public string MyText
    {
        get { return textBox1.Text; }
        set 
        {
            if (textBox1.Text != value)
            {
                textBox1.Text = value;
                OnMyTextChanged();
            }
        }
    }

    MyClass myClass { get; set; }

    public Form1()
    {
        InitializeComponent();
        myClass = new MyClass();
        Binding binding = new Binding("MyText", myClass, "Dic");
        binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        binding.Parse += new ConvertEventHandler(binding_Parse);
        binding.Format += new ConvertEventHandler(binding_Format);
        DataBindings.Add(binding);
        myClass.AddStuff("uno", "UNO");
    }

    void OnMyTextChanged()
    {
        if (MyTextChanged != null) MyTextChanged(this, EventArgs.Empty);
    }

    void binding_Format(object sender, ConvertEventArgs e)
    {
        if (e.Value is Dictionary<string, string>)
        {
            Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
            e.Value = source.Count.ToString();
        }
    }

    void binding_Parse(object sender, ConvertEventArgs e)
    {
        MessageBox.Show(e.DesiredType.ToString());

    }

    private void changemyClassButton_Click(object sender, EventArgs e)
    {
        myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
    }

    private void changeMyTextButton_Click(object sender, EventArgs e)
    {
        MyText = "1234";
    }
}

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Dictionary<string, string> Dic { get; set; }

    public MyClass()
    {
        Dic = new Dictionary<string, string>();
    }

    public void AddStuff(string key, string value)
    {
        Dic.Add(key, value);
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
    }
}
+2

, , Parse.

binding_Parse, :

public partial class Form1 : Form
{

    public MyClass myClass { get; set; }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myClass = new MyClass();
        myClass.AddStuff("uno", "UNO");

        Binding b = new Binding("Text", myClass, "Dic");
        b.Parse += new ConvertEventHandler(b_Parse);
        b.Format += new ConvertEventHandler(b_Format);
        textBox1.DataBindings.Add(b);
    }

    void b_Format(object sender, ConvertEventArgs e)
    {
        e.Value = (e.Value as Dictionary<string, string>)["uno"].ToString();
        textBox1.Text = e.Value.ToString();
    }

    void b_Parse(object sender, ConvertEventArgs e)
    {
        MessageBox.Show("This is executed when you lost focus\n\nI'm parsing your entered text: " + e.Value);
    }


}

. , Format. , textbox1.Text, , , Parse.

, .

+1

Source: https://habr.com/ru/post/1770796/


All Articles