Remarks: This is a direct binding, in that the get/set delegates can get/set the value directly without any associated object instance. This is used when binding directly to a property or when an object instance isn't needed to get/set a value.
Type parameters:
| TValue |
Constructors
DelegateBinding
#public DelegateBinding()
Initializes a new instance of the DelegateBinding<TValue> class.
DelegateBinding
#public DelegateBinding(Func<TValue> getValue = null, Action<TValue> setValue = null, Action<EventHandler<EventArgs>> addChangeEvent = null, Action<EventHandler<EventArgs>> removeChangeEvent = null)
Initializes a new instance of the DelegateBinding<TValue> class with the specified delegates.
Parameters:
| getValue (Func<TValue> = null) | Delegate to get the value for the binding. |
| setValue (Action<TValue> = null) | Delegate to set the value for the binding. |
| addChangeEvent (Action<EventHandler<EventArgs>> = null) | Delegate to register the change event, when needed by the consumer of this binding. |
| removeChangeEvent (Action<EventHandler<EventArgs>> = null) | Delegate to remove the change event. |
Properties
AddChangeEvent
#Gets or sets the delegate to register the change event, when needed by the consumer of this binding.
Value: The add change event delegate.
RemoveChangeEvent
#Gets or sets the delegate to remove the change event.
Value: The remove change event delegate.
Methods
Cast<TValue>
Casts this binding value to another (compatible) type.
CatchException
Catches any exceptions when setting the value of the binding
CatchException<TException>
Catches any exceptions of the specified
TException when setting the value of the binding.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<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 |
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) |
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.
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
Unbind
public virtual void Unbind()
Unbind this from the target object(s)
Remarks: Typically a binding may handle an event for when the target object(s) property values are changed. This is called to unbind the binding from the objects so that they can be garbage collected
Implements: Unbind
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