Struct BindingValue<T>
A value passed into a binding.
public readonly struct BindingValue<T> : IEquatable<BindingValue<T>>
Type Parameters
T
The value type.
- Implements
- Inherited Members
Remarks
The avalonia binding system is typed, and as such additional state is stored in this structure. A binding value can be in a number of states, described by the Type property:
- Value: a simple value
- UnsetValue: the target property will revert to its unbound state until a new binding value is produced. Represented by UnsetValue in an untyped context
- DoNothing: the binding value will be ignored. Represented by DoNothing in an untyped context
- BindingError: a binding error, such as a missing source property, with an optional fallback value
- DataValidationError: a data validation error, with an optional fallback value
To create a new binding value you can:
- For a simple value, call the BindingValue<T> constructor or use an implicit
conversion from
T
- For an unset value, use Unset or simply
default
- For other types, call one of the static factory methods
Constructors
BindingValue(T)
Initializes a new instance of the BindingValue<T> struct with a type of Value
public BindingValue(T value)
Parameters
value
TThe value.
Properties
DoNothing
Returns a binding value with a type of DoNothing.
public static BindingValue<T> DoNothing { get; }
Property Value
- BindingValue<T>
Error
Gets the binding or data validation error.
public Exception? Error { get; }
Property Value
HasError
Gets a value indicating whether the binding value represents either a binding or data validation error.
public bool HasError { get; }
Property Value
HasValue
Gets a value indicating whether the binding value has a value.
public bool HasValue { get; }
Property Value
Type
Gets the type of the binding value.
public BindingValueType Type { get; }
Property Value
Unset
Returns a binding value with a type of UnsetValue.
public static BindingValue<T> Unset { get; }
Property Value
- BindingValue<T>
Value
Gets the binding value or fallback value.
public T Value { get; }
Property Value
- T
Exceptions
- InvalidOperationException
HasValue is false.
Methods
BindingError(Exception)
Returns a binding value with a type of BindingError.
public static BindingValue<T> BindingError(Exception e)
Parameters
e
ExceptionThe binding error.
Returns
- BindingValue<T>
BindingError(Exception, Optional<T>)
Returns a binding value with a type of BindingError or BindingErrorWithFallback.
public static BindingValue<T> BindingError(Exception e, Optional<T> fallbackValue)
Parameters
Returns
- BindingValue<T>
BindingError(Exception, T)
Returns a binding value with a type of BindingErrorWithFallback.
public static BindingValue<T> BindingError(Exception e, T fallbackValue)
Parameters
e
ExceptionThe binding error.
fallbackValue
TThe fallback value.
Returns
- BindingValue<T>
DataValidationError(Exception)
Returns a binding value with a type of DataValidationError.
public static BindingValue<T> DataValidationError(Exception e)
Parameters
e
ExceptionThe data validation error.
Returns
- BindingValue<T>
DataValidationError(Exception, Optional<T>)
Returns a binding value with a type of DataValidationError or DataValidationErrorWithFallback.
public static BindingValue<T> DataValidationError(Exception e, Optional<T> fallbackValue)
Parameters
Returns
- BindingValue<T>
DataValidationError(Exception, T)
Returns a binding value with a type of DataValidationErrorWithFallback.
public static BindingValue<T> DataValidationError(Exception e, T fallbackValue)
Parameters
e
ExceptionThe data validation error.
fallbackValue
TThe fallback value.
Returns
- BindingValue<T>
Equals(BindingValue<T>)
public bool Equals(BindingValue<T> other)
Parameters
other
BindingValue<T>
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
obj
object
Returns
FromUntyped(object?)
Creates a BindingValue<T> from an object, handling the special values UnsetValue, DoNothing and BindingNotification.
public static BindingValue<T> FromUntyped(object? value)
Parameters
value
objectThe untyped value.
Returns
- BindingValue<T>
The typed binding value.
FromUntyped(object?, Type)
Creates a BindingValue<T> from an object, handling the special values UnsetValue, DoNothing and BindingNotification.
public static BindingValue<T> FromUntyped(object? value, Type targetType)
Parameters
Returns
- BindingValue<T>
The typed binding value.
GetHashCode()
public override int GetHashCode()
Returns
GetValueOrDefault()
Gets the value of the binding value if present, otherwise the default value.
public T? GetValueOrDefault()
Returns
- T
The value.
GetValueOrDefault(T)
Gets the value of the binding value if present, otherwise a default value.
public T? GetValueOrDefault(T defaultValue)
Parameters
defaultValue
TThe default value.
Returns
- T
The value.
GetValueOrDefault<TResult>()
Gets the value if present, otherwise the default value.
public TResult? GetValueOrDefault<TResult>()
Returns
- TResult
The value if present and of the correct type,
default(TResult)
if the value is not present or of an incorrect type.
Type Parameters
TResult
GetValueOrDefault<TResult>(TResult)
Gets the value of the binding value if present, otherwise a default value.
public TResult? GetValueOrDefault<TResult>(TResult defaultValue)
Parameters
defaultValue
TResultThe default value.
Returns
- TResult
The value if present and of the correct type,
default(TResult)
if the value is present but not of the correct type or null, ordefaultValue
if the value is not present.
Type Parameters
TResult
ToOptional()
Converts the binding value to an Optional<T>.
public Optional<T> ToOptional()
Returns
- Optional<T>
ToString()
public override string ToString()
Returns
ToUntyped()
Converts the value to untyped representation, using UnsetValue, DoNothing and BindingNotification where appropriate.
public object? ToUntyped()
Returns
- object
The untyped representation of the binding value.
WithValue(T)
Returns a new binding value with the specified value.
public BindingValue<T> WithValue(T value)
Parameters
value
TThe new value.
Returns
- BindingValue<T>
The new binding value.
Exceptions
- InvalidOperationException
The binding type is UnsetValue or DoNothing.
Operators
operator ==(BindingValue<T>, BindingValue<T>)
public static bool operator ==(BindingValue<T> left, BindingValue<T> right)
Parameters
left
BindingValue<T>right
BindingValue<T>
Returns
implicit operator BindingValue<T>(Optional<T>)
Creates a binding value from an Optional<T>.
public static implicit operator BindingValue<T>(Optional<T> optional)
Parameters
optional
Optional<T>The optional value.
Returns
- BindingValue<T>
implicit operator BindingValue<T>(T)
Creates a binding value from an instance of the underlying value type.
public static implicit operator BindingValue<T>(T value)
Parameters
value
TThe value.
Returns
- BindingValue<T>
operator !=(BindingValue<T>, BindingValue<T>)
public static bool operator !=(BindingValue<T> left, BindingValue<T> right)
Parameters
left
BindingValue<T>right
BindingValue<T>