DelegateBinding<T, TValue>

IndirectBinding<1>Binding
public class DelegateBinding<T, TValue> : IndirectBinding<TValue>
Indirect binding using delegate methods
Remarks: This is an indirect binding, in that the object to get/set the values from/to is passed to each of the delegates to get/set the value. This is used for things like columns in a Grid control to bind to specific values of each item in the grid.
TType of the object this binding will get/set the values from/to
TValueType of the value this binding will get/set

Constructors

DelegateBinding

#
public DelegateBinding(Func<T, TValue> getValue = null, Action<T, TValue> setValue = null, Func<T, EventHandler<EventArgs>, object> addChangeEvent = null, Action<object, EventHandler<EventArgs>> removeChangeEvent = null, TValue defaultGetValue = default(TValue), TValue defaultSetValue = default(TValue))
Initializes a new instance of the DelegateBinding<T, TValue> class.
getValue (Func<T, TValue> = null)Delegate to get the value for the binding.
setValue (Action<T, TValue> = null)Delegate to set the value for the binding.
addChangeEvent (Func<T, EventHandler<EventArgs>, object> = null)Delegate to register the change event, when needed by the consumer of this binding.
removeChangeEvent (Action<object, EventHandler<EventArgs>> = null)Delegate to remove the change event.
defaultGetValue (TValue = null)Default get value, when the object instance is null.
defaultSetValue (TValue = null)Default set value, when the incoming value is null.

DelegateBinding

#
public DelegateBinding(Func<T, TValue> getValue, Action<T, TValue> setValue, Action<T, EventHandler<EventArgs>> addChangeEvent, Action<T, EventHandler<EventArgs>> removeChangeEvent, TValue defaultGetValue = default(TValue), TValue defaultSetValue = default(TValue))
Initializes a new instance of the DelegateBinding<T, TValue> class.
getValue (Func<T, TValue>)Delegate to get the value for the binding.
setValue (Action<T, TValue>)Delegate to set the value for the binding.
addChangeEvent (Action<T, EventHandler<EventArgs>>)Delegate to register the change event, when needed by the consumer of this binding.
removeChangeEvent (Action<T, EventHandler<EventArgs>>)Delegate to remove the change event.
defaultGetValue (TValue = null)Default get value, when the object instance is null.
defaultSetValue (TValue = null)Default set value, when the incoming value is null.

DelegateBinding

#
public DelegateBinding(Func<T, TValue> getValue, Action<T, TValue> setValue, string notifyProperty, TValue defaultGetValue = default(TValue), TValue defaultSetValue = default(TValue))
Initializes a new instance of the DelegateBinding<T, TValue> class.
getValue (Func<T, TValue>)Delegate to get the value for the binding.
setValue (Action<T, TValue>)Delegate to set the value for the binding.
notifyProperty (string)Name of the property to listen for change events of this binding.
defaultGetValue (TValue = null)Default get value, when the object instance is null.
defaultSetValue (TValue = null)Default set value, when the incoming value is null.

Properties

AddChangeEvent

#
public Func<T, EventHandler<EventArgs>, object> AddChangeEvent { get; set; }
Gets or sets the delegate to register the change event, when needed by the consumer of this binding.

DefaultGetValue

#
public TValue DefaultGetValue { get; set; }
Gets or sets the default get value, when the object instance is null.

DefaultSetValue

#
public TValue DefaultSetValue { get; set; }
Gets or sets the default set value, when the incoming value is null.

GetValue

#
public Func<T, TValue> GetValue { get; set; }
Gets or sets the delegate to get the value for this binding.

RemoveChangeEvent

#
public Action<object, EventHandler<EventArgs>> RemoveChangeEvent { get; set; }
Gets or sets the delegate to remove the change event.

SetValue

#
public Action<T, TValue> SetValue { get; set; }
Gets or sets the delegate to set the value for this binding.

Methods

AddValueChangedHandler

#
public override object AddValueChangedHandler(object dataItem, EventHandler<EventArgs> handler)
Wires an event handler to fire when the property of the dataItem is changed
dataItem (object)object to detect changes on
handler (EventHandler<EventArgs>)handler to fire when the property changes on the specified dataItem
binding reference used to track the event hookup, to pass to RemoveValueChangedHandler(object, EventHandler<EventArgs>) when removing the handler

AfterDelay

public IndirectBinding<T> AfterDelay(double delay, bool reset = false)
Specifies that the binding should only respond to change events after a delay.
inherited fromIndirectBinding<T>
Remarks: This is useful if the property/delegate that is bound is expensive to retrieve the new value, for example to dynamically generate a bitmap based on the state of the model, etc. The reset boolean allows you to ensure that the binding is updated periodically when false (default), or true to wait for the delay period after the last change event.
delay (double)The delay, in seconds to wait after the value has changed before updating the binding.
reset (bool = false)true to reset the delay every time the event is fired, false to trigger the change at least by the delay interval since the last time it was triggered
A binding that will delay the change event

AfterDelay

public IndirectBinding<T> AfterDelay(TimeSpan delay, bool reset = false)
Specifies that the binding should only respond to change events after a delay.
inherited fromIndirectBinding<T>
Remarks: This is useful if the property/delegate that is bound is expensive to retrieve the new value, for example to dynamically generate a bitmap based on the state of the model, etc. The reset boolean allows you to ensure that the binding is updated periodically when false (default), or true to wait for the delay period after the last change event.
delay (TimeSpan)The delay time span to wait after the value has changed before updating the binding.
reset (bool = false)true to reset the delay every time the event is fired, false to trigger the change at least by the delay interval since the last time it was triggered
A binding that will delay the change event

CatchException

public IndirectBinding<T> CatchException(Func<Exception, bool> exceptionHandler = null)
Catches any exceptions when setting the value of the binding
inherited fromIndirectBinding<T>
exceptionHandler (Func<Exception, bool> = null)Handler to call when setting the value, regardless of whether an exception occurs. Return true when the exception is handled, false to throw an exception.
The binding that catches any exception.

CatchException<TException>

public IndirectBinding<T> CatchException<TException>(Func<TException, bool> exceptionHandler = null)
Catches any exceptions of the specified TException when setting the value of the binding.
inherited fromIndirectBinding<T>
exceptionHandler (Func<TException, bool> = null)Handler to call when setting the value, regardless of whether an exception occurs. Return true when the exception is handled, false to throw an exception.
The binding that catches the specified exception.

Child<TNewValue>

public IndirectBinding<TNewValue> Child<TNewValue>(Expression<Func<T, TNewValue>> property)
Binds to the specified child property expression.
inherited fromIndirectBinding<T>
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.
property (Expression<Func<T, TNewValue>>)Property to bind to.
The binding to the child property accessed through the current binding.
Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }

Binding.Property((MyModel m) => m.ChildObject).Child(c => c.SomeChildProperty);
// or:
Binding.Property((MyModel m) => m.ChildObject.SomeChildProperty);

Child<TNewValue>

public IndirectBinding<TNewValue> Child<TNewValue>(IndirectBinding<TNewValue> binding)
Binds to the specified child binding of this binding.
inherited fromIndirectBinding<T>
Remarks: This can be used to bind to child objects of your view model, for example
model.SomeProperty.ChildProperty
.
binding (IndirectBinding<TNewValue>)Binding to get the child value from this binding.
The binding to the child property accessed through the current binding.
Use this like so:
public class MyChild { public SomeChildProperty { get; set; } }
public class MyModel { public ChildObject { get; set; } }

Binding.Property((MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));

Convert

public IndirectBinding<object> Convert(IValueConverter converter, Type propertyType, object conveterParameter = null, CultureInfo culture = null)
Converts the binding using the specified converter object.
inherited fromIndirectBinding<T>
converter (IValueConverter)Converter object to use when converting to/from the value
propertyType (Type)Type for the converter to convert to
conveterParameter (object = null)Parameter to pass to the converter.
culture (CultureInfo = null)Culture to use for conversion, null to use invariant culture.
A new binding that will be converted using the specified IValueConverter.

Convert<TValue>

public IndirectBinding<TValue> Convert<TValue>(Func<T, TValue> getValue, Action<T, TValue> setValue)
Converts this binding's value to another value using delegates.
inherited fromIndirectBinding<T>
Remarks: This differs from IndirectBinding<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.
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.
A new binding that will be converted using the specified delegates.

Convert<TValue>

public IndirectBinding<TValue> Convert<TValue>(Func<T, TValue> toValue, Func<TValue, T> fromValue = null)
Converts this binding's value to another value using delegates.
inherited fromIndirectBinding<T>
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.
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.

Convert<TValue>

public IndirectBinding<TValue> Convert<TValue>(IValueConverter converter, object conveterParameter = null, CultureInfo culture = null)
Converts the binding using the specified converter object.
inherited fromIndirectBinding<T>
converter (IValueConverter)Converter object to use when converting to/from the value
conveterParameter (object = null)Parameter to pass to the converter.
culture (CultureInfo = null)Culture to use for conversion, null to use invariant culture.
A new binding that will be converted using the specified IValueConverter.

EnumToString

public IndirectBinding<string> EnumToString(T defaultValue = default(T))
Converts the a binding to an enumeration to/from its string representation
inherited fromIndirectBinding<T>
defaultValue (T = null)Default if the value is not valid or empty.
Binding to the string value of the enumeration.

GetValue

public T GetValue(object dataItem)
Gets the value from the specified object using this binding
inherited fromIndirectBinding<T>
Remarks: When values are needed from this binding, this method will be called. Implementors of this binding would implement logic in IndirectBinding<T>.InternalGetValue(object)
dataItem (object)object to retrieve the value from
value from the specified object
GetValue

HandleEvent

protected virtual void HandleEvent(string id)
Called to handle an event for this binding
inherited fromBinding

InternalGetValue

#
protected override TValue InternalGetValue(object dataItem)
Implements the logic to get the value from the specified object
Remarks: Implementors of this binding must implement this method to get the value from the specified object
dataItem (object)object to get the value from
value from this binding of the specified object

InternalSetValue

#
protected override void InternalSetValue(object dataItem, TValue value)
Implements the logic to set the value to the specified object
dataItem (object)object to set the value to
value (TValue)value to set on the dataItem for this binding

OfType<TValue>

public IndirectBinding<TValue> OfType<TValue>(TValue defaultConvertedValue = default(TValue), T defaultValue = default(T))
Casts this binding value to another (compatible) type, or returns the default if the types do not match.
inherited fromIndirectBinding<T>

RemoveEvent

protected virtual void RemoveEvent(string id)
Called to remove an event for this binding
inherited fromBinding

RemoveValueChangedHandler

#
public override void RemoveValueChangedHandler(object bindingReference, EventHandler<EventArgs> handler)
Removes the handler for the specified reference from AddValueChangedHandler(object, EventHandler<EventArgs>)

SetValue

public void SetValue(object dataItem, T value)
Sets the specified value to an object using this binding
inherited fromIndirectBinding<T>
Remarks: This is called to set the value to the object. Implementors of this binding woulc implement logic in IndirectBinding<T>.InternalSetValue(object, T).
dataItem (object)object to set the value to
value (T)value to set to the object
SetValue

ToBool

public IndirectBinding<bool?> ToBool(T trueValue, T falseValue, T nullValue)
Converts this binding to return a nullable boolean binding
inherited fromIndirectBinding<T>
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.
trueValue (T)Value when the binding is true.
falseValue (T)Value when the binding is false.
nullValue (T)Value when the binding is null.
Boolean binding.

ToBool

public IndirectBinding<bool?> ToBool(T trueValue, T falseValue)
Converts this binding to return a nullable boolean binding
inherited fromIndirectBinding<T>
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.
trueValue (T)Value when the binding is true.
falseValue (T)Value when the binding is false.
Boolean binding.

ToBool

public IndirectBinding<bool?> ToBool(T trueValue)
Converts this binding to return a nullable boolean binding
inherited fromIndirectBinding<T>
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.
trueValue (T)Value when the binding is true.
Boolean binding.

ToType<TType>

public IndirectBinding<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.
inherited fromIndirectBinding<T>
Remarks: This has additional logic to deal with nullable types so they can be converted properly as well.
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.
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)
inherited fromBinding
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
Unbind

Update

public virtual void Update(BindingUpdateMode mode = BindingUpdateMode.Destination)
Updates the bound target object's value
inherited fromBinding
Remarks: Typically the source would be your custom class and the destination would be a UI control, but this is not always the case.
mode (BindingUpdateMode = BindingUpdateMode.Destination)Direction of the update
Update

Events

Changing

public event EventHandler<BindingChangingEventArgs> Changing
Event to handle when the value is being set using this binding
inherited fromBinding
Remarks: This can be used to stop a value being updated based on custom logic