Table of Contents

Struct BindingValue<T>

Namespace
Avalonia.Data
Assembly
Avalonia.Base.dll

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 T

The 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

Exception

HasError

Gets a value indicating whether the binding value represents either a binding or data validation error.

public bool HasError { get; }

Property Value

bool

HasValue

Gets a value indicating whether the binding value has a value.

public bool HasValue { get; }

Property Value

bool

Type

Gets the type of the binding value.

public BindingValueType Type { get; }

Property Value

BindingValueType

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 Exception

The 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

e Exception

The binding error.

fallbackValue Optional<T>

The fallback value.

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 Exception

The binding error.

fallbackValue T

The 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 Exception

The 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

e Exception

The binding error.

fallbackValue Optional<T>

The fallback value.

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 Exception

The data validation error.

fallbackValue T

The fallback value.

Returns

BindingValue<T>

Equals(BindingValue<T>)

public bool Equals(BindingValue<T> other)

Parameters

other BindingValue<T>

Returns

bool

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

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 object

The 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

value object

The untyped value.

targetType Type

The runtime target type.

Returns

BindingValue<T>

The typed binding value.

GetHashCode()

public override int GetHashCode()

Returns

int

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 T

The 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 TResult

The 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, or defaultValue 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

string

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 T

The 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

bool

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 T

The 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>

Returns

bool