Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:16:44+00:00 2026-05-17T20:16:44+00:00

I’m trying to understand better how data binding works in .net. I was checking

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:16:45+00:00Added an answer on May 17, 2026 at 8:16 pm

    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"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.