- ControlBinding<T, TValue>
Remarks: This provides control-specific binding, such as binding to a IBindable.DataContext. Any bindings created using this will also add to the IBindable.Bindings collection to keep its reference.
Type parameters:
| T | where IBindable |
| TValue |
Constructors
BindableBinding
#public BindableBinding(T dataItem, Func<T, TValue> getValue, Action<T, TValue> setValue = null, Action<T, EventHandler<EventArgs>> addChangeEvent = null, Action<T, EventHandler<EventArgs>> removeChangeEvent = null)
Initializes a new instance of the ControlBinding<T, TValue> class.
Parameters:
| dataItem (T) | Data item to get/set the values from/to. |
| getValue (Func<T, TValue>) | Delegate to get the value from the object. |
| setValue (Action<T, TValue> = null) | Delegate to set the value to the object. |
| addChangeEvent (Action<T, EventHandler<EventArgs>> = null) | Delegate to add the change event. |
| removeChangeEvent (Action<T, EventHandler<EventArgs>> = null) | Delegate to remove the chang event. |
BindableBinding
#Initializes a new instance of the ControlBinding<T, TValue> class.
Parameters:
| dataItem (T) | Control the binding is attached to. |
| innerBinding (IndirectBinding<TValue>) | Inner binding. |
Properties
DataItem
public T DataItem { get; set; }
Gets or sets the object to get/set the values using the ObjectBinding<T, TValue>.InnerBinding
Remarks: This uses ObjectBinding<T, TValue>.GetDataItem if set, otherwise it will use the set value. Setting the value explicitly will set ObjectBinding<T, TValue>.GetDataItem to null.
DataValue
public override TValue DataValue { get; set; }
Gets or sets the value of this binding on the bound object
Remarks: This uses the ObjectBinding<T, TValue>.InnerBinding on the ObjectBinding<T, TValue>.DataItem to get/set the value
Overrides: DirectBinding<T>.DataValue
GetDataItem
Gets or sets a delegate used to get the current ObjectBinding<T, TValue>.DataItem value instead of storing the value in this binding.
Remarks: This is used so that all bindings using a DataContext will be updated with the correct value as soon as the DataContext is set. For example, if updating one binding would trigger the setter of a secondary binding that hasn't been updated with the new value yet it would inadvertently change the DataContext even though it is now actually null. Use the ObjectBinding<T, TValue>.TriggerDataValueChanged() method to specify that the value returned by this delegate has changed, which will cause the binding to update all of its dependants. Note if this is set, then it will always be used to retrieve the DataItem. If you set the DataItem directly, this will be set to null and will no longer be used..
Value: The delegate used to get the current data item
GettingNullValue
public TValue GettingNullValue { get; set; }
Gets or sets the default value to use when getting the value for this binding when the ObjectBinding<T, TValue>.DataItem or property value is null
inherited fromObjectBinding<T, TValue>
SettingNullValue
public TValue SettingNullValue { get; set; }
Gets or sets the default value to use when setting the value for this binding when input value is null
Methods
Bind
#public override DualBinding<TValue> Bind(DirectBinding<TValue> sourceBinding, DualBindingMode mode = DualBindingMode.TwoWay)
Binds the specified
sourceBinding to this binding.Remarks: This creates a DualBinding<TValue> between the specified
sourceBinding and this binding. The binding is added to the IBindable.Bindings collection.Parameters:
| sourceBinding (DirectBinding<TValue>) | Source binding to bind from. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
Overrides: ObjectBinding<T, TValue>.Bind
Bind
public DualBinding<TValue> Bind(Func<TValue> getValue, Action<TValue> setValue = null, Action<EventHandler<EventArgs>> addChangeEvent = null, Action<EventHandler<EventArgs>> removeChangeEvent = null, DualBindingMode mode = DualBindingMode.TwoWay)
Creates a new dual binding using a DelegateBinding<TValue> with the specified delegates and this binding.
Remarks: This creates a DualBinding<TValue> between a new DelegateBinding<TValue> and this binding. This does not require an object instance for the delegates to get/set the value. You must keep a reference to the binding to unbind when finished.
Parameters:
| getValue (Func<TValue>) | Delegate to get the value. |
| setValue (Action<TValue> = null) | Delegate to set the value when changed. |
| addChangeEvent (Action<EventHandler<EventArgs>> = null) | Delegate to add a change event when the value changes. |
| removeChangeEvent (Action<EventHandler<EventArgs>> = null) | Delegate to remove the change event. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
Bind
public DualBinding<TValue> Bind(object objectValue, string propertyName, DualBindingMode mode = DualBindingMode.TwoWay)
Creates a binding to the
propertyName of the specified objectValue.Remarks: This is a shortcut to using the PropertyBinding<TValue>. This has the advantage of registering automatically to INotifyPropertyChanged or to an event named after the property with a "Changed" suffix.
Parameters:
| objectValue (object) | Object to bind to. |
| propertyName (string) | Name of the property to bind to on the objectValue. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Direction of the binding. |
Bind<TObject>
public DualBinding<TValue> Bind<TObject>(TObject objectValue, Expression<Func<TObject, TValue>> propertyExpression, DualBindingMode mode = DualBindingMode.TwoWay)
Creates a binding to the specified
objectValue with the specified propertyExpression.Remarks: This has the advantage of registering automatically to INotifyPropertyChanged or to an event named after the property with a "Changed" suffix, if the expression is a property. When the expression does not evaluate to a property, it will not be able to bind to the changed events and will use the expression as a delegate directly.
Parameters:
| objectValue (TObject) | Object to bind to. |
| propertyExpression (Expression<Func<TObject, TValue>>) | Expression for a property of the objectValue, or a non-property expression with no change event binding. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Direction of the binding |
Returns: The binding between the data context and this binding
Bind<TObject>
public DualBinding<TValue> Bind<TObject>(TObject objectValue, IndirectBinding<TValue> objectBinding, DualBindingMode mode = DualBindingMode.TwoWay)
Creates a new dual binding between the specified
objectBinding and this binding.Parameters:
| objectValue (TObject) | Object to get/set the values from/to. |
| objectBinding (IndirectBinding<TValue>) | Indirect binding to get/set the values from the objectValue. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
BindDataContext
#public DualBinding<TValue> BindDataContext(IndirectBinding<TValue> dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, TValue defaultControlValue = default(TValue), TValue defaultContextValue = default(TValue))
Binds to an object's IBindable.DataContext using the specified
dataContextBinding.Remarks: This creates a DualBinding<TValue> between a binding to the specified
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 IBindable.Bindings collection.Parameters:
| dataContextBinding (IndirectBinding<TValue>) | Binding to get/set values from/to the control's data context. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
| defaultControlValue (TValue = null) | Default control value. |
| defaultContextValue (TValue = null) | Default context value. |
Returns: A new dual binding that binds the
dataContextBinding to this control binding.BindDataContext
#public DualBinding<TValue> BindDataContext(string propertyName, DualBindingMode mode = DualBindingMode.TwoWay)
Binds to the specified
propertyName of the current data context.Remarks: This is a shortcut to using the PropertyBinding<TValue>. This has the advantage of registering automatically to INotifyPropertyChanged or to an event named after the property with a "Changed" suffix.
Parameters:
| propertyName (string) | Name of the property on the data context to bind to. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Direction of the binding. |
Returns: The binding between the data context and this binding.
BindDataContext<TObject>
#public DualBinding<TValue> BindDataContext<TObject>(Expression<Func<TObject, TValue>> propertyExpression, DualBindingMode mode = DualBindingMode.TwoWay)
Binds to a specified property of the control's current data context.
Remarks: This has the advantage of registering automatically to INotifyPropertyChanged or to an event named after the property with a "Changed" suffix, if the expression is a property. When the expression does not evaluate to a property, it will not be able to bind to the changed events and will use the expression as a delegate directly.
Parameters:
| propertyExpression (Expression<Func<TObject, TValue>>) | Expression for a property of the data context, or a non-property expression with no change event binding. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Direction of the binding |
Returns: The binding between the data context and this binding
BindDataContext<TObject>
#public DualBinding<TValue> BindDataContext<TObject>(Func<TObject, TValue> getValue, Action<TObject, TValue> setValue, Action<TObject, EventHandler<EventArgs>> addChangeEvent = null, Action<TObject, EventHandler<EventArgs>> removeChangeEvent = null, DualBindingMode mode = DualBindingMode.TwoWay, TValue defaultGetValue = default(TValue), TValue defaultSetValue = default(TValue))
Binds to a control's IBindable.DataContext using delegates to get/set the value.
Remarks: This is a shortcut to use the DelegateBinding<T, TValue> to bind to a control's IBindable.DataContext property. When the data context type is
TValue, then the delegates will be called to get/set the value. Otherwise, if the data context is null or a different type, the defaultGetValue will be used.Parameters:
| getValue (Func<TObject, TValue>) | Delegate to get the value from the data context. |
| setValue (Action<TObject, TValue>) | Delegate to set the value to the data context when changed. |
| addChangeEvent (Action<TObject, EventHandler<EventArgs>> = null) | Delegate to add a change event on the data context. |
| removeChangeEvent (Action<TObject, EventHandler<EventArgs>> = null) | Delegate to remove the change event from the data context. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
| defaultGetValue (TValue = null) | Default get value. |
| defaultSetValue (TValue = null) | Default set value. |
Returns: A new dual binding that binds the control to this object binding.
CatchException
#Catches any exceptions when setting the value of the binding
CatchException<TException>
#public BindableBinding<T, TValue> CatchException<TException>(Func<TException, bool> exceptionHandler = null)
Catches any exceptions of the specified
TException when setting the value of the binding.Child<TNewValue>
#Binds to the specified child
property expression.Remarks: This can be used to bind to properties of child objects of your view model, for example
model.SomeProperty.ChildProperty. This will automatically look up the changed event either by a [Property]Changed event or INotifyPropertyChanged implementation for each object in the heirarchy. Note that you only really need to use this when you have an existing binding that you cannot change. See Binding.Property<T, TValue>(Expression<Func<T, TValue>>) for an example of how to bind to child property values more directly.Parameters:
| property (Expression<Func<T, TNewValue>>) | Property to bind to. |
Returns: The binding to the child property accessed through the current binding.
Example:Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }
var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(c => c.SomeChildProperty);Child<TNewValue>
#Binds to the specified child
binding of this binding.Remarks: This can be used to bind to child objects of your view model, for example
model.SomeProperty.ChildProperty.Parameters:
| binding (IndirectBinding<TNewValue>) | Binding to get the child value from this binding. |
Returns: The binding to the child property accessed through the current binding.
Example:Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }
var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));Child<TValue>
Binds to the specified child
property expression.Remarks: This can be used to bind to properties of child objects of your view model, for example
model.SomeProperty.ChildProperty. This will automatically look up the changed event either by a [Property]Changed event or INotifyPropertyChanged implementation for each object in the heirarchy. Note that you only really need to use this when you have an existing binding that you cannot change. See Binding.Property<T, TValue>(Expression<Func<T, TValue>>) for an example of how to bind to child property values more directly.Parameters:
| property (Expression<Func<T, TValue>>) | Property to bind to. |
Returns: The binding to the child property accessed through the current binding.
Example:Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }
var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(c => c.SomeChildProperty);Child<TValue>
Binds to the specified child
binding of this binding.Remarks: This can be used to bind to child objects of your view model, for example
model.SomeProperty.ChildProperty.Parameters:
| binding (IndirectBinding<TValue>) | Binding to get the child value from this binding. |
Returns: The binding to the child property accessed through the current binding.
Example:Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }
var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));Convert<TNewValue>
#public BindableBinding<T, TNewValue> Convert<TNewValue>(Func<TValue, TNewValue> toValue, Func<TNewValue, TValue> fromValue = null)
Converts this binding's value to another value using delegates.
Remarks: This is useful when you want to cast one binding to another, perform logic when getting/setting a value from a particular binding, or get/set a preoperty of the value.
Convert<TValue>
Converts this binding's value to another value using delegates.
Remarks: This differs from DirectBinding<T>.Convert<TValue>(Func<T, TValue>, Func<TValue, T>) in that it allows the set value delegate to have access to the original parent value, which can be useful when needing to call a method of the parent binding instead of converting the value directly, or performing other logic.
Parameters:
| getValue (Func<T, TValue>) | Delegate to get the value from the original binding's type. |
| setValue (Action<T, TValue>) | Delegate to set the value to the original binding's type. |
Returns: A new binding with the specified
TValue type.Convert<TValue>
public DirectBinding<TValue> Convert<TValue>(Func<T, TValue> toValue, Func<TValue, T> fromValue = null)
Converts this binding's value to another value using delegates.
Remarks: This is useful when you want to cast one binding to another, perform logic when getting/setting a value from a particular binding, or get/set a preoperty of the value.
Parameters:
| toValue (Func<T, TValue>) | Delegate to convert to the new value type. |
| fromValue (Func<TValue, T> = null) | Delegate to convert from the value to the original binding's type. |
Returns: A new binding with the specified
TValue type.Convert<TValue>
public DirectBinding<TValue> Convert<TValue>(IValueConverter converter, object conveterParameter = null, CultureInfo culture = null)
Converts this binding's value to another value using an IValueConverter.
Parameters:
| converter (IValueConverter) | The value converter to use. |
| conveterParameter (object = null) | An optional parameter to pass to the converter. |
| culture (CultureInfo = null) | An optional culture to use for the conversion. |
Returns: A new binding with the specified
TValue type.Exceptions:
| ArgumentNullException |
HandleEvent
Hooks up the late bound events for this object
inherited fromBinding
inherited fromBinding
inherited fromDirectBinding<T>
Remarks: Implementors of this class should call this method when the value changes on the bound object. Make sure to also override the HandleEvent and RemoveEvent methods to hook up/remove any event bindings you need on the bound object.
Parameters:
| e (EventArgs) |
RemoveEvent
Removes the late bound events for this object
ToBool
Converts this binding to return a nullable boolean binding
Remarks: This is useful when converting a binding to be used for a checkbox's Checked binding for example. When the binding's value matches the
trueValue, it will return true.Parameters:
| trueValue (T) | Value when the binding is true. |
| falseValue (T) | Value when the binding is false. |
| nullValue (T) | Value when the binding is null. |
Returns: Boolean binding.
ToBool
Converts this binding to return a nullable boolean binding
Remarks: This is useful when converting a binding to be used for a checkbox's Checked binding for example. When the binding's value matches the
trueValue, it will return true.Parameters:
| trueValue (T) | Value when the binding is true. |
| falseValue (T) | Value when the binding is false or null. |
Returns: Boolean binding.
ToBool
Converts this binding to return a nullable boolean binding
Remarks: This is useful when converting a binding to be used for a checkbox's Checked binding for example. When the binding's value matches the
trueValue, it will return true.Parameters:
| trueValue (T) | Value when the binding is true, false, or null. |
Returns: Boolean binding.
ToString
Returns a String that represents the current ObjectBinding.
Returns: A String that represents the current ObjectBinding.
Overrides: Object.ToString
ToType<TType>
public DirectBinding<TType> ToType<TType>(Func<T, TType> invalidGetValue = null, Func<TType, T> invalidSetValue = null)
Uses System.Convert.ChangeType to change the value of the binding to the specified type.
Remarks: This has additional logic to deal with nullable types so they can be converted properly as well.
Parameters:
| invalidGetValue (Func<T, TType> = null) | Delegate to get a value when it cannot be converted to the specified TType. When null, an exception will be thrown when the value cannot be converted. |
| invalidSetValue (Func<TType, T> = null) | Delegate to set a value when it cannot be converted from the specified TType. When null, an exception will be thrown when the value cannot be converted. |
Returns: A binding of the new type that is a converted version of this binding
TriggerDataValueChanged
public void TriggerDataValueChanged()
Triggers the DirectBinding<T>.DataValueChanged event.
Remarks: When using the ObjectBinding<T, TValue>.GetDataItem delegate to retrieve the value of the DataItem, you can call this method to trigger that its value has been changed.
Unbind
public override void Unbind()
Unbinds this binding
Update
Updates the bound target object's value
Remarks: Typically the source would be your custom class and the destination would be a UI control, but this is not always the case.
Parameters:
| mode (BindingUpdateMode = BindingUpdateMode.Destination) | Direction of the update |
Implements: Update
Events
Changed
Event to handle after the value has been set using this binding
Changing
Event to handle when the value is being set using this binding
Remarks: This can be used to stop a value being updated based on custom logic
DataValueChanged
Event to handle when the value changes on the bound object