/// <summary> /// Binds to a control's <see cref="Control.DataContext"/> using the specified <paramref name="dataContextBinding"/>. /// </summary> /// <remarks> /// This creates a <see cref="DualBinding{TValue}"/> between a binding to the specified <paramref name="dataContextBinding"/> and this binding. /// Since the data context changes, the binding passed for the data context binding is an indirect binding, in that it is reused. /// The binding is added to the <see cref="Control.Bindings"/> collection. /// </remarks> /// <returns>A new dual binding that binds the <paramref name="dataContextBinding"/> to this control binding.</returns> /// <param name="dataContextBinding">Binding to get/set values from/to the control's data context.</param> /// <param name="mode">Dual binding mode.</param> /// <param name="defaultControlValue">Default control value.</param> /// <param name="defaultContextValue">Default context value.</param> public DualBinding <TValue> BindDataContext(IndirectBinding <TValue> dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, TValue defaultControlValue = default(TValue), TValue defaultContextValue = default(TValue)) { var control = DataItem; if (control == null) { throw new InvalidOperationException("Binding must be attached to a control"); } var contextBinding = new ControlBinding <Control, object>(control, new DelegateBinding <Control, object>(w => w.DataContext, null, (w, h) => w.DataContextChanged += h, (w, h) => w.DataContextChanged -= h)); var valueBinding = new ObjectBinding <object, TValue>(control.DataContext, dataContextBinding) { GettingNullValue = defaultControlValue, SettingNullValue = defaultContextValue, DataItem = contextBinding.DataValue }; DualBinding <TValue> binding = Bind(sourceBinding: valueBinding, mode: mode); contextBinding.DataValueChanged += delegate { ((ObjectBinding <object, TValue>)binding.Source).DataItem = contextBinding.DataValue; }; control.Bindings.Add(contextBinding); return(binding); }
/// <summary> /// Adds a new binding to the control with a direct value binding /// </summary> /// <param name="controlBinding">Binding to get/set the value from the control.</param> /// <param name="valueBinding">Value binding to get/set the value from another source.</param> /// <param name="mode">Mode of the binding</param> public DualBinding <T> Bind <T>(IndirectBinding <T> controlBinding, DirectBinding <T> valueBinding, DualBindingMode mode = DualBindingMode.TwoWay) { var binding = new ControlBinding <Control, T>(this, controlBinding); return(binding.Bind(sourceBinding: valueBinding, mode: mode)); }
/// <summary> /// Adds a new binding from the control to its data context /// </summary> /// <param name="controlBinding">Binding to get/set the value from the control.</param> /// <param name="dataContextBinding">Binding to get/set the value from the <see cref="Control.DataContext"/>.</param> /// <param name="mode">Mode of the binding.</param> /// <param name="defaultControlValue">Default control value to set to the objectValue, if the value of the control property is null.</param> /// <param name="defaultContextValue">Default context value to set to the control, if the objectValue or value of the objectBinding is null.</param> public DualBinding <T> BindDataContext <T>(IndirectBinding <T> controlBinding, IndirectBinding <T> dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, T defaultControlValue = default(T), T defaultContextValue = default(T)) { var binding = new ControlBinding <Control, T>(this, controlBinding); return(binding.BindDataContext(dataContextBinding, mode, defaultControlValue, defaultContextValue)); }