DirectBinding<T>

BindingReferences
public abstract class DirectBinding<T> : Binding
Abstraction to get/set values from a provided object
Remarks: This binding provides a way to get/set values of an object that is provided by the binding, and not passed in. This differs from the IndirectBinding<T>, which requires that the caller pass in the object to get/set the value from/to.
T

Fields

DataValueChangedEvent

#
public const string DataValueChangedEvent = "ObjectBinding.DataValueChangedEvent"
Identifier for the DataValueChanged event

Properties

DataValue

#
public abstract T DataValue { get; set; }
Gets or sets the value of this binding

Methods

Cast<TValue>

#
public DirectBinding<TValue> Cast<TValue>()
Casts this binding value to another (compatible) type.

CatchException

#
public DirectBinding<T> CatchException(Func<Exception, bool> exceptionHandler = null)
Catches any exceptions when setting the value of the binding
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 DirectBinding<T> CatchException<TException>(Func<TException, bool> exceptionHandler = null)
Catches any exceptions of the specified TException when setting the value of the binding.
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<TValue>

#
public DirectBinding<TValue> Child<TValue>(Expression<Func<T, TValue>> property)
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.
property (Expression<Func<T, TValue>>)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; } }

var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(c => c.SomeChildProperty);

Child<TValue>

#
public DirectBinding<TValue> Child<TValue>(IndirectBinding<TValue> binding)
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
.
binding (IndirectBinding<TValue>)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; } }

var model = new MyModel();
Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));

Convert<TValue>

#Since v2.11
public DirectBinding<TValue> Convert<TValue>(Func<T, TValue> getValue, Action<T, TValue> setValue)
Converts this binding's value to another value using delegates.
Remarks: This differs from 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 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.
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.
A new binding with the specified TValue type.

Convert<TValue>

#Since v2.11
public DirectBinding<TValue> Convert<TValue>(IValueConverter converter, object conveterParameter = null, CultureInfo culture = null)
Converts this binding's value to another value using an IValueConverter.
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.
A new binding with the specified TValue type.

HandleEvent

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

OnDataValueChanged

#
protected virtual void OnDataValueChanged(EventArgs e)
Handles the DataValueChanged event
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.
e (EventArgs)

RemoveEvent

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

ToBool

#
public DirectBinding<bool?> ToBool(T trueValue, T falseValue, T nullValue)
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.
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 DirectBinding<bool?> ToBool(T trueValue, T falseValue)
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.
trueValue (T)Value when the binding is true.
falseValue (T)Value when the binding is false or null.
Boolean binding.

ToBool

#
public DirectBinding<bool?> ToBool(T trueValue)
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.
trueValue (T)Value when the binding is true, false, or null.
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.
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

DataValueChanged

#
public event EventHandler<EventArgs> DataValueChanged
Event to handle when the value changes on the bound object