I'm trying to understand better how data binding works in .net. I was checking 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"));
}
}
I'm trying to bind MyText
to myClass
. The problem is that the function binding_Parse
is never being called. I know I could probably bind textBox1.Text
directly to myClass
, or that there might be a thousand other possible ways to do what I'm trying to do, but this is just a practice; I'm trying to understand better data binding. So I want to bind a custom object to a custom property so I can see the process from end to end. The custom object is myClass
, and the custom property is MyText
. I've tried all kinds of variations, like implementing INotifyPropertyChanged
, but I can't get binding_Parse
to be called (I would expect it to be called when I call changeMyTextButton_Click
). Am I missing something?
Edit:
To put it simpler: I want to write a user control with a property string MyText
that then a user can bind to something else, the same way you can bind a TextBox
's Text
property to something else. So I don't want to bind to the property of a control to an object, I want to write a control with a property that then a user can bind to an object.