Strong typed Windows Forms databinding

前端 未结 3 336
轻奢々
轻奢々 2021-01-02 14:34

I am looking into strong typed Windows Forms databinding using extension methods. I have got this far following help from Xavier as below:

using System;
usin         


        
3条回答
  •  生来不讨喜
    2021-01-02 14:48

    I have been using the code posted by Stuart for a few months now. I did add a few more overloads to match the rest of the databinding scenarios that you may want to use (I'm just posting it here for others to have an even easier time getting this very useful thing working)

        public static class ControlExtensions {
    
        /// Databinding with strongly typed object names
        /// The Control you are binding to
        /// The property on the control you are binding to
        /// The object you are binding to
        /// The property on the object you are binding to
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty));
        }
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty, bool formattingEnabled = false)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled);
        }
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode);
        }
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue);
        }
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString);
        }
        public static Binding Bind(this TControl control, Expression> controlProperty, object dataSource, Expression> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString, IFormatProvider formatInfo)
        where TControl :Control {
            return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString, formatInfo);
        }
    
        public static class PropertyName {
            public static string For(Expression> property) {
                var member = property.Body as MemberExpression;
                if(null == member) {
                    var unary = property.Body as UnaryExpression;
                    if(null != unary) member = unary.Operand as MemberExpression;
                }
                return null != member ? member.Member.Name : string.Empty;
            }
        }
    
    }
    

提交回复
热议问题