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

  • Home
  • SEARCH
  • 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 368935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:52:17+00:00 2026-05-12T13:52:17+00:00

This probably applies to other places, but in WinForms, when I use binding I

  • 0

This probably applies to other places, but in WinForms, when I use binding I find many methods want to take the name of the property to bind to. Something like:

class Person
{
    public String Name { get { ... } set { ... } }
    public int Age { get { ... } set { ... } }
}

class PersonView
{
    void Bind(Person p)
    {
        nameControl.Bind(p,"Name");
        ageControl.Bind(p,"Age");
    }
}

The big problem I keep having with this is that “Name” and “Age” are specified as strings. This means the compiler is no help if someone renames one of Person’s properties. The code will compile fine, but the bindings will be broken.

Is there a standard way of solving this that I’ve missed? It feels like I need some keyword, maybe called stringof to match the existing typeof. You could use it something like:

ageControl.Bind(p,stringof(p.Age).Name);

stringof could return some class that has properties for getting the full path, part of the path, or the string so you can parse it up yourself.

Is something like this already do-able?

  • 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-12T13:52:17+00:00Added an answer on May 12, 2026 at 1:52 pm

    You can use Expressions to get compiler-checked bindings.
    For example, in one of current projects we set up bindings like this:

    DataBinder
        .BindToObject(this)
        .ObjectProperty(c => c.IsReadOnly)
            .Control(nameTextBox, n => n.ReadOnly)
            .Control(addressControl, n => n.ReadOnly)
    

    Code supporting this style is separated into several classes:

    public static class DataBinder
    {
        public static DataBinderBindingSourceContext<TDataSource> BindToObject<TDataSource>(TDataSource dataSource)
        {
            return new DataBinderBindingSourceContext<TDataSource>(dataSource);
        }
    }
    
    public class DataBinderBindingSourceContext<TDataSource> 
    {
        public readonly object DataSource;
    
        public DataBinderBindingSourceContext(object dataSource)
        {
            DataSource = dataSource;
        }
    
        public DataBinderControlContext<TDataSource, TProperty> ObjectProperty<TProperty>(Expression<Func<TDataSource, TProperty>> property)
        {
            return new DataBinderControlContext<TDataSource, TProperty>(this, property);
        }
    }
    
    public class DataBinderControlContext<TDataSource, TProperty>
    {
        readonly DataBinderBindingSourceContext<TDataSource> BindingSourceContext;
        readonly string ObjectProperty;
    
        public DataBinderControlContext
            (
                DataBinderBindingSourceContext<TDataSource> bindingSourceContext,
                Expression<Func<TDataSource, TProperty>> objectProperty
            )
        {
            BindingSourceContext = RequireArg.NotNull(bindingSourceContext);
            ObjectProperty = ExpressionHelper.GetPropertyName(objectProperty);
        }
    
        public DataBinderControlContext<TDataSource, TProperty> Control<TControl>(TControl control, Expression<Func<TControl, TProperty>> property)
            where TControl : Control
        {
            var controlPropertyName = ExpressionHelper.GetPropertyName(property);
            control.DataBindings.Add(controlPropertyName, BindingSourceContext.DataSource, ObjectProperty, true);
    
            return this;
        }
    }
    
    public static class ExpressionHelper
    {
        public static string GetPropertyName<TResult>(Expression<Func<TResult>> property)
        {
            return GetMemberNames(((LambdaExpression)property).Body).Skip(1).Join(".");
        }
    
        public static string GetPropertyName<T, TResult>(Expression<Func<T, TResult>> property)
        {
            return GetMemberNames(((LambdaExpression)property).Body).Join(".");
        }
    
        static IEnumerable<string> GetMemberNames(Expression expression)
        {
            if (expression is ConstantExpression || expression is ParameterExpression)
                yield break;
    
            var memberExpression = (MemberExpression)expression;
    
            foreach (var memberName in GetMemberNames(memberExpression.Expression))
                yield return memberName;
    
            yield return memberExpression.Member.Name;
        }
    }
    
    public static class StringExtentions
    {
        public static string Join(this IEnumerable<string> values, string separator)
        {
            if (values == null)
                return null;
    
            return string.Join(separator, values.ToArray());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 235k
  • Answers 235k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You left out the @encode directive: [layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer… May 13, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer Sets As a principle I never say try [anySearhEngine] on… May 13, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer The term handle generally means some opaque value that has… May 13, 2026 at 6:05 am

Related Questions

When setting up foreign keys in SQL Server, under what circumstances should you have
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Having a look around at WebsiteSpark from Microsoft - the deal is good (especially
When I want to prevent other event handlers from executing after a certain event

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.