示例#1
0
        /// <summary>
        /// Binds a control property to a <see cref="BindableWidget.DataContext"/> property
        /// </summary>
        /// <param name="control">Control to bind to.</param>
        /// <param name="controlProperty">Control property.</param>
        /// <param name="sourceProperty">Source property from the data context.</param>
        /// <param name="mode">Mode of the binding.</param>
        /// <param name="defaultControlValue">Default control value, if the control value is null.</param>
        /// <param name="defaultContextValue">Default context value, if the context value is null.</param>
        /// <typeparam name="TWidget">The type of control.</typeparam>
        /// <typeparam name="TContext">The type of the data context object.</typeparam>
        /// <typeparam name="TValue">The type of the property.</typeparam>
        public static DualBinding <TValue> BindDataContext <TWidget, TContext, TValue>(this TWidget control, Expression <Func <TWidget, TValue> > controlProperty, Expression <Func <TContext, TValue> > sourceProperty, DualBindingMode mode = DualBindingMode.TwoWay, TValue defaultControlValue = default(TValue), TValue defaultContextValue = default(TValue))
            where TWidget : IBindable
        {
            var controlExpression = controlProperty.GetMemberInfo();
            IndirectBinding <TValue> controlBinding;

            if (controlExpression != null)
            {
                controlBinding = new PropertyBinding <TValue>(controlExpression.Member.Name);
            }
            else
            {
                controlBinding = new DelegateBinding <TWidget, TValue>(controlProperty.Compile());
            }

            var sourceExpression = sourceProperty.GetMemberInfo();
            IndirectBinding <TValue> sourceBinding;

            if (sourceExpression != null)
            {
                sourceBinding = new PropertyBinding <TValue>(sourceExpression.Member.Name);
            }
            else
            {
                sourceBinding = new DelegateBinding <TContext, TValue>(sourceProperty.Compile());
            }
            return(control.BindDataContext(controlBinding, sourceBinding, mode, defaultControlValue, defaultContextValue));
        }
示例#2
0
 public DualBinding <TValue> Bind <TObject>(DelegateBinding <TObject, TValue> binding, DualBindingMode mode = DualBindingMode.TwoWay)
 {
     return(Bind(dataContextBinding: binding, mode: mode));
 }