ObjectBinding<TValue>
ObjectBinding<2>DirectBinding<1>BindingBinding for a particular object to get/set values from/to
Remarks: This binding provides a way to get/set values for a particular object. This uses a IndirectBinding<T> as its logic to actually retrieve/set the values. This acts as a bridge between the IndirectBinding<T> and DirectBinding<T> so that you can utilize the DirectBinding<T>.DataValueChanged method. Typically, one would use the PropertyBinding<T>, or the ObjectBinding{T,TValue} constructor to hook up this binding to a particular property of the specified object
Type parameters:
| TValue | The type of value for the binding. |
Constructors
ObjectBinding
#Initializes a new instance of the ObjectBinding with the specified object and binding to get/set values with
Parameters:
| dataItem (object) | object to get/set values from |
| innerBinding (IndirectBinding<TValue>) | binding to use to get/set the values from the dataItem |
ObjectBinding
#Initializes a new instance of the ObjectBinding with the specified object and property for a PropertyBinding<T>
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 virtual DualBinding<TValue> Bind(DirectBinding<TValue> sourceBinding, DualBindingMode mode = DualBindingMode.TwoWay)
Creates a new dual binding between the specified
sourceBinding and this binding.Remarks: This creates a DualBinding<TValue> between the specified
sourceBinding and this binding. You must keep a reference to the binding to unbind when finished.Parameters:
| sourceBinding (DirectBinding<TValue>) | Source binding to bind from. |
| mode (DualBindingMode = DualBindingMode.TwoWay) | Dual binding mode. |
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. |
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 |
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