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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:16:31+00:00 2026-06-01T10:16:31+00:00

I have the following classes… class ExpressionBinder<T> { public Func<T> Getter; public Action<T> Setter;

  • 0

I have the following classes…

class ExpressionBinder<T>
{
    public Func<T> Getter;
    public Action<T> Setter;

    public T Value
    {
        get { return Getter.Invoke(); }
        set { Setter.Invoke(value); }
    }

    public ExpressionBinder(Func<T> getter, Action<T> setter)
    {
        Getter = getter;
        Setter = setter;
    }
}

class ComparisonBinder<TSource, TValue> : ExpressionBinder<bool>, INotifyPropertyChanged
{
    private TSource instance;
    private TValue comparisonValue;
    private PropertyInfo pInfo;

    public event PropertyChangedEventHandler PropertyChanged;

    public ComparisonBinder(TSource instance, Expression<Func<TSource, TValue>> property, TValue comparisonValue) : base(null,null)
    {
        pInfo = GetPropertyInfo(property);

        this.instance = instance;
        this.comparisonValue = comparisonValue;

        Getter = GetValue;
        Setter = SetValue;
    }

    private bool GetValue()
    {
        return comparisonValue.Equals(pInfo.GetValue(instance, null));
    }

    private void SetValue(bool value)
    {
        if (value)
        {
            pInfo.SetValue(instance, comparisonValue,null);

            NotifyPropertyChanged("CustomerName");
        }
    }

    private void NotifyPropertyChanged(string pName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(pName));
        }
    }

    /// <summary>
    /// Adapted from surfen's answer (https://stackoverflow.com/a/10003320/219838)
    /// </summary>
    private PropertyInfo GetPropertyInfo(Expression<Func<TSource, TValue>> propertyLambda)
    {
        Type type = typeof(TSource);

        MemberExpression member = propertyLambda.Body as MemberExpression;

        if (member == null)
            throw new ArgumentException(string.Format(
                "Expression '{0}' refers to a method, not a property.",
                propertyLambda));

        PropertyInfo propInfo = member.Member as PropertyInfo;
        if (propInfo == null)
            throw new ArgumentException(string.Format(
                "Expression '{0}' refers to a field, not a property.",
                propertyLambda));

        if (type != propInfo.ReflectedType &&
            !type.IsSubclassOf(propInfo.ReflectedType))
            throw new ArgumentException(string.Format(
                "Expresion '{0}' refers to a property that is not from type {1}.",
                propertyLambda,
                type));

        return propInfo;
    }
}

My code using the ComparisonBinder class is as follows:

radioMale.DataBindings.Add(
    "Checked", 
    new ComparisonBinder<DataClass, GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Male), 
    "Value", 
    false, 
    DataSourceUpdateMode.OnPropertyChanged);

radioFemale.DataBindings.Add(
    "Checked",
    new ComparisonBinder<DataClass, GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Male), 
    "Value", 
    false, 
    DataSourceUpdateMode.OnPropertyChanged);

They allows me to bind many controls to the same property using an expression to generate the value for the control’s bound property. It works beautifully from the control to the object. (If you want to see more about using this class and a this question)

Now, I need to get the other way around. When I update my object (which will alter the result of the get expression) I need the bound control to be updated. I tried implementing the INotifyPropertyChanged but it didn’t make any difference.

One odd thing: setting the controls DataSourceUpdateMode to OnPropertyChanged somehow stops me from changing the checked radio.

  • 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-06-01T10:16:32+00:00Added an answer on June 1, 2026 at 10:16 am

    Quick answer:

    • You HAVE TO use binding source
    • Implement INotifyPropertyChanged in your object
    • Make the proxy listen to the event above
    • Implement INotifyPropertyChanged in the proxy
    • Filter the PropertyChanged events received from your object in the proxy and raise only when ((PropertyChangedEventArgs) args).PropertyName == pInfo.Name
    • Add the proxy to BindSource.DataSource
    • Use the BindingSource to bind the control to your data object’s property

    The final code

    Available (along with other binding classes) at: http://github.com/svallory/BindingTools

    Classes

    class ExpressionBinder<T> : INotifyPropertyChanged
    {
        public Func<T> Getter;
        public Action<T> Setter;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected IList<string> WatchingProps;
    
        public T Value
        {
            get { return Getter.Invoke(); }
            set { Setter.Invoke(value); }
        }
    
        public ExpressionBinder(Func<T> getter, Action<T> setter)
        {
            WatchingProps = new List<string>();
            Getter = getter;
            Setter = setter;
        }
    
        public ExpressionBinder(Func<T> getter, Action<T> setter, ref PropertyChangedEventHandler listenToChanges, IList<string> propertyNames)
        {
            Getter = getter;
            Setter = setter;
    
            listenToChanges += SourcePropertyChanged;
            WatchingProps = propertyNames;
        }
    
        protected void SourcePropertyChanged(object obj, PropertyChangedEventArgs args)
        {
            if (PropertyChanged != null && WatchingProps.Contains(args.PropertyName))
            {
                PropertyChanged(this, args);
            }
        }
    
        protected PropertyInfo GetPropertyInfo<TSource, TValue>(Expression<Func<TSource, TValue>> propertyLambda)
        {
            Type type = typeof(TSource);
    
            var member = propertyLambda.Body as MemberExpression;
    
            if (member == null)
                throw new ArgumentException(string.Format(
                    "Expression '{0}' refers to a method, not a property.",
                    propertyLambda));
    
            var propInfo = member.Member as PropertyInfo;
            if (propInfo == null)
                throw new ArgumentException(string.Format(
                    "Expression '{0}' refers to a field, not a property.",
                    propertyLambda));
    
            if (type != propInfo.ReflectedType &&
                !type.IsSubclassOf(propInfo.ReflectedType))
                throw new ArgumentException(string.Format(
                    "Expresion '{0}' refers to a property that is not from type {1}.",
                    propertyLambda,
                    type));
    
            return propInfo;
        }
    }
    
    class ComparisonBinder<TSource, TValue> : ExpressionBinder<bool> where TSource : INotifyPropertyChanged
    {
        private readonly TSource instance;
        private readonly TValue comparisonValue;
        private readonly PropertyInfo pInfo;
    
        public ComparisonBinder(TSource instance, Expression<Func<TSource, TValue>> property, TValue comparisonValue)
            : base(null, null)
        {
            pInfo = GetPropertyInfo(property);
    
            this.instance = instance;
            this.comparisonValue = comparisonValue;
    
            Getter = GetValue;
            Setter = SetValue;
    
            instance.PropertyChanged += SourcePropertyChanged;
            WatchingProps.Add(pInfo.Name);
        }
    
        private bool GetValue()
        {
            return comparisonValue.Equals(pInfo.GetValue(instance, null));
        }
    
        private void SetValue(bool value)
        {
            if (value)
            {
                pInfo.SetValue(instance, comparisonValue, null);
            }
        }
    }
    

    Usage

    var bs = new BindingSource();
    bs.DataSource = new ComparisonBinder<MySourceObjClass, PropType>(
        MySourceObject, 
        p => p.PropertyToBind, 
        ValueToCompare);
    
    rdBeSmart.DataBindings.Add(
        "Checked", 
        bs, 
        "Value");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
We have following classes public class MyPropertyBase { public int StartOffset { get; set;
I have the following classes: public class Menu { public int Order { get;
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have the following classes: class Person { public string Name { get; set;
I have the following classes class A{ private String name; private int value; public
I have following classes public class Person { public string FirstName { get; set;
I have the following classes: public class Note { public string Text { get;
I have the following classes: public class Truck { public Wheel Wheel { get;
I have following classes: public abstract class CustomerBase { public long CustomerNumber { get;

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.