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 653343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:22:18+00:00 2026-05-13T22:22:18+00:00

My question is : how to move beyond writing a custom implementation of a

  • 0

My question is : how to move beyond writing a custom implementation of a technique for databinding multiple controls (controls without built-in DataSource properties), for each possible type of data, to simple properties … as described and demonstrated in code that follows … to achieve a more poweful solution that will be independent of whether the binding is to a string, or an int, or other types.

My guess is: this will involve reflection; but, I’m stuck at that point. I’m looking for strategic advice on which “direction” to move next, hints, clues, not a complete code answer, but of course I appreciate all responses, and I’ll sure study code if you post code in reply ! Marc Clifton’s 2005 article on CodeProject Simple Databinding: appears to demonstrate a reflection based approach: but, honestly, I do not really grok his code, and, in terms of .NET, 2005 is a long time ago.

Background: Partly in response to various SO questions and answers, like: Update Usercontrol on Three Forms: I’ve evolved a successful technique for databinding text properties of various controls simultaneously to one source defined in a Public class; also been able to “abstract” some of the details of the binding process using a static class that defines one extension method, and two public methods.

I’ve verifed that TextBoxes on Controls in a “MainForm,” TextBoxes on a UserControl on the MainForm, and a TextBox on a second Form opened “independently” (i.e., form2.Parent == null) all update properly (i.e., two-way binding is in effect) from the “DataSource equivalent” public class. Change one: change all.

Code: an instance of this class will supply the target property (theText) for databinding:

    public class TextDataBinder
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _theText;

        public string theText
        {
            get { return _theText; }

            // note : if 'setter is declared 'internal : blocks 
            // auto-updating when run-time user modifies consumers
            // but will still allow update via code
            set
            {
                _theText = value;
                OnPropertyChanged(new PropertyChangedEventArgs("theText"));         
            }
        }

        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }

Code: this static class enables hiding some of the binding process complexity, and allows easy binding to multiple controls:

    public static class TextBindingExtender
    {
        public static TextDataBinder CurrentDataSource;

        public static void SetCurrentDataSource(TextDataBinder newCurrentDataSource)
        {
            CurrentDataSource = newCurrentDataSource;
        }

        // extension method for Control
        public static void AddTextBinding(this Control theControl, string controlPropertyName, string targetPropertyName)
        {
            theControl.DataBindings.Add(controlPropertyName, CurrentDataSource, targetPropertyName, false, DataSourceUpdateMode.OnPropertyChanged);
        }

        // bind to all Controls in a List<Control>
        public static void AddTextBindings(List<Control> theControls, string controlPropertyName, string targetPropertyName)
        {
            foreach (Control theControl in theControls)
            {
                theControl.AddTextBinding(controlPropertyName, targetPropertyName);
            }
        }
    }

How the above classes are used (in a Form Load event) :

    // create a new TextDataBinder
    TextBindingExtender.CurrentDataSource = new TextDataBinder();

    // bind to multiple textboxes, label, on a UserControl, on another Form, etc.
    TextBindingExtender.AddTextBindings(new List<Control> { textBox1, textBox2, userControl11.tb, label1, instanceOfForm2.tb }, "Text", "theText");

    // test assigning some initial text to the bound property
    TextBindingExtender.CurrentDataSource.theText = "some initial text";
  • 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-13T22:22:18+00:00Added an answer on May 13, 2026 at 10:22 pm

    It really depends what you want to do; but ultimately common data-binding (for simple properties, done manually) consists of:

    • obtaining a property; preferably via TypeDescriptor.GetProperties(obj)[propName], giving you an abstraction (PropertyDescriptor)
    • asking the property if it is read-only (.IsReadOnly)
    • obtain (or set) the value (.GetValue(), .SetValue())
    • asking it for a converter to format / parse the value (.Converter, .ConvertFromString(), .ConvertToString()) THIS is a key bit that means you don’t have to worry about what the data type is
    • asking it for the caption (.DisplayName, or .Name if that it empty/null)
    • asking it if it supports property-specific notification (.SupportsChangeEvents)
    • asking it to add/remove a change handler (.AddValueChanged(), .RemoveValueChanged())
    • you might also want to look at whether the object supports centralised notification (look for INotifyPropertyChanged)

    If you might be binding to a list rather than a single object:
    – the list might be abstracted behind IListSource
    – the list might have custom properties, so check for ITypedList
    – otherwise, identify the Type of the items and use TypeDescriptor.GetProperties(type)
    – you need to consider a “currency manager” (i.e. should all the things bound to the same list be pointing to the same record in the list all the time)

    There are also things like ICustomTypeDescriptor and TypeDescriptionProvider to consider, but most of the time TypeDescriptor handles this for you automatically.

    As you can see – lots of things to think about! Lots of work… the one thing that you don’t have to do is reflection; this is abstracted behind PropertyDescriptor. The reason for this is that not all data is static-typed; think about DataTable – the columns (which map to bindable data properties) are not fixed at compile-time, so reflection isn’t appropriate. Likewise, some other types have custom “property bag” implementations. PropertyDescriptor lets your code handle either dynamic (not in the 4.0 sense) and reflective properties identically. It also works nicely with things like “HyperDescriptor”, another property customisation.

    • 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.