Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think this can be done using <code>PropertyInfo</code> and some lambda magic:</p> <pre><code>class ComparisonBinder&lt;TSource,TValue&gt; : ExpressionBinder&lt;bool&gt; { public ComparisonBinder(TSource source, Expression&lt;Func&lt;TSource, bool&gt;&gt; propertyLambda, TValue comparisonValue) :base(null,null) { var propertyInfo = GetPropertyInfo&lt;TSource,bool&gt;(source, propertyLambda); this.Getter = () =&gt; comarisonValue.Equals((TValue)propertyInfo.GetValue(source)); this.Setter = (bool value) =&gt; { if(value) propertyInfo.SetValue(source, comparisonValue); }; } } </code></pre> <p>Usage:</p> <pre><code>radioMale.DataBindings.Add("Checked", new ComparisonBinder&lt;ConfigClass, GenderEnum&gt;(config, c =&gt; c.Gender, GenderEnum.Male), "Value"); radioFemale.DataBindings.Add("Checked", new ComparisonBinder&lt;ConfigClass, GenderEnum&gt;(config, c =&gt; c.Gender, GenderEnum.Female), "Value"); </code></pre> <p>I have not tested it but hopefully you'll be able to fix any errors and use this solution.</p> <p>GetPropertyInfo(): copied from <a href="https://stackoverflow.com/a/672212/724944">https://stackoverflow.com/a/672212/724944</a></p> <pre><code>public PropertyInfo GetPropertyInfo&lt;TSource, TProperty&gt;( TSource source, Expression&lt;Func&lt;TSource, TProperty&gt;&gt; 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.ToString())); PropertyInfo propInfo = member.Member as PropertyInfo; if (propInfo == null) throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", propertyLambda.ToString())); if (type != propInfo.ReflectedType &amp;&amp; !type.IsSubclassOf(propInfo.ReflectedType)) throw new ArgumentException(string.Format( "Expresion '{0}' refers to a property that is not from type {1}.", propertyLambda.ToString(), type)); return propInfo; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload