views:

71

answers:

2

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.

A: 

Perhaps this will help you out, understanding when Parse event is executed.

To see binding_Parse working check out this sample:

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);
    }


}

You just have debug. Click on the button, and notice Format event is called first. Then if you manually change textbox1.Text value, when you click on the button again, there will be a lost focus, and then Parse event will be executed.

To create your custom control also check this site.

Junior Mayhé
Please see my edit. I apologize for the confusion. Hope you can still help me.
jsoldi
Ok did you see the site I told you?
Junior Mayhé
I did but I couldn't find anything about binding properties; they use events instead.
jsoldi
A: 

OK I figured it out in case anyone had the same problem. I had to create an event handler named MyTextChanged to let the Binding knows MyText is changing, and set the Bindings DataSourceUpdateMode property to OnPropertyChanged. Using this simple principle I can bind a pixel in my screen to the rest of the universe :). Here's the code:

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"));
    }
}
jsoldi
What I still don't understand is why is this not causing and infinite recursion. I guess there is some internal mechanism to prevent that.
jsoldi