Editar

Compartilhar via


ObservableObject.SetProperty Method

Definition

Overloads

SetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, Action<T>, String).

SetProperty<T>(T, T, IEqualityComparer<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, String).

SetProperty<T>(T, T, Action<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. This overload is much less efficient than SetProperty<T>(T, T, String) and it should only be used when the former is not viable (eg. when the target property being updated does not directly expose a backing field that can be passed by reference). For performance reasons, it is recommended to use a stateful callback if possible through the SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String) whenever possible instead of this overload, as that will allow the C# compiler to cache the input callback and reduce the memory allocations. More info on that overload are available in the related XML docs. This overload is here for completeness and in cases where that is not applicable.

SetProperty<T>(T, T, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event.

SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String)

Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(T, T, String), with the difference being that this method is used to relay properties from a wrapped model in the current instance. This type is useful when creating wrapping, bindable objects that operate over models that lack support for notification (eg. for CRUD operations). Suppose we have this model (eg. for a database row in a table):

public class Person
{
    public string Name { get; set; }
}

We can then use a property to wrap instances of this type into our observable model (which supports notifications), injecting the notification to the properties of that model, like so:

public class BindablePerson : ObservableObject
{
    public Model { get; }

    public BindablePerson(Person model)
    {
        Model = model;
    }

    public string Name
    {
        get => Model.Name;
        set => Set(Model.Name, value, Model, (model, name) => model.Name = name);
    }
}

This way we can then use the wrapping object in our application, and all those "proxy" properties will also raise notifications when changed. Note that this method is not meant to be a replacement for SetProperty<T>(T, T, String), and it should only be used when relaying properties to a model that doesn't support notifications, and only if you can't implement notifications to that model directly (eg. by having it inherit from ObservableObject). The syntax relies on passing the target model and a stateless callback to allow the C# compiler to cache the function, which results in much better performance and no memory usage.

SetProperty<TModel,T>(T, T, IEqualityComparer<T>, TModel, Action<TModel,T>, String)

Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(T, T, String), with the difference being that this method is used to relay properties from a wrapped model in the current instance. See additional notes about this overload in SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String).

SetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, Action<T>, String).

protected bool SetProperty<T> (T oldValue, T newValue, System.Collections.Generic.IEqualityComparer<T> comparer, Action<T> callback, string? propertyName = default);
member this.SetProperty : 'T * 'T * System.Collections.Generic.IEqualityComparer<'T> * Action<'T> * string -> bool
Protected Function SetProperty(Of T) (oldValue As T, newValue As T, comparer As IEqualityComparer(Of T), callback As Action(Of T), Optional propertyName As String = Nothing) As Boolean

Type Parameters

T

The type of the property that changed.

Parameters

oldValue
T

The current property value.

newValue
T

The property's value after the change occurred.

comparer
IEqualityComparer<T>

The IEqualityComparer<T> instance to use to compare the input values.

callback
Action<T>

A callback to invoke to update the property value.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Applies to

SetProperty<T>(T, T, IEqualityComparer<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, String).

protected bool SetProperty<T> (ref T field, T newValue, System.Collections.Generic.IEqualityComparer<T> comparer, string? propertyName = default);
member this.SetProperty : 'T * 'T * System.Collections.Generic.IEqualityComparer<'T> * string -> bool
Protected Function SetProperty(Of T) (ByRef field As T, newValue As T, comparer As IEqualityComparer(Of T), Optional propertyName As String = Nothing) As Boolean

Type Parameters

T

The type of the property that changed.

Parameters

field
T

The field storing the property's value.

newValue
T

The property's value after the change occurred.

comparer
IEqualityComparer<T>

The IEqualityComparer<T> instance to use to compare the input values.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Applies to

SetProperty<T>(T, T, Action<T>, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. This overload is much less efficient than SetProperty<T>(T, T, String) and it should only be used when the former is not viable (eg. when the target property being updated does not directly expose a backing field that can be passed by reference). For performance reasons, it is recommended to use a stateful callback if possible through the SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String) whenever possible instead of this overload, as that will allow the C# compiler to cache the input callback and reduce the memory allocations. More info on that overload are available in the related XML docs. This overload is here for completeness and in cases where that is not applicable.

protected bool SetProperty<T> (T oldValue, T newValue, Action<T> callback, string? propertyName = default);
member this.SetProperty : 'T * 'T * Action<'T> * string -> bool
Protected Function SetProperty(Of T) (oldValue As T, newValue As T, callback As Action(Of T), Optional propertyName As String = Nothing) As Boolean

Type Parameters

T

The type of the property that changed.

Parameters

oldValue
T

The current property value.

newValue
T

The property's value after the change occurred.

callback
Action<T>

A callback to invoke to update the property value.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Remarks

The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.

Applies to

SetProperty<T>(T, T, String)

Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event.

protected bool SetProperty<T> (ref T field, T newValue, string? propertyName = default);
member this.SetProperty : 'T * 'T * string -> bool
Protected Function SetProperty(Of T) (ByRef field As T, newValue As T, Optional propertyName As String = Nothing) As Boolean

Type Parameters

T

The type of the property that changed.

Parameters

field
T

The field storing the property's value.

newValue
T

The property's value after the change occurred.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Remarks

The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.

Applies to

SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String)

Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(T, T, String), with the difference being that this method is used to relay properties from a wrapped model in the current instance. This type is useful when creating wrapping, bindable objects that operate over models that lack support for notification (eg. for CRUD operations). Suppose we have this model (eg. for a database row in a table):

public class Person
{
    public string Name { get; set; }
}

We can then use a property to wrap instances of this type into our observable model (which supports notifications), injecting the notification to the properties of that model, like so:

public class BindablePerson : ObservableObject
{
    public Model { get; }

    public BindablePerson(Person model)
    {
        Model = model;
    }

    public string Name
    {
        get => Model.Name;
        set => Set(Model.Name, value, Model, (model, name) => model.Name = name);
    }
}

This way we can then use the wrapping object in our application, and all those "proxy" properties will also raise notifications when changed. Note that this method is not meant to be a replacement for SetProperty<T>(T, T, String), and it should only be used when relaying properties to a model that doesn't support notifications, and only if you can't implement notifications to that model directly (eg. by having it inherit from ObservableObject). The syntax relies on passing the target model and a stateless callback to allow the C# compiler to cache the function, which results in much better performance and no memory usage.

protected bool SetProperty<TModel,T> (T oldValue, T newValue, TModel model, Action<TModel,T> callback, string? propertyName = default) where TModel : class;
member this.SetProperty : 'T * 'T * 'Model * Action<'Model, 'T (requires 'Model : null)> * string -> bool (requires 'Model : null)
Protected Function SetProperty(Of TModel As Class, T As Class) (oldValue As T, newValue As T, model As TModel, callback As Action(Of TModel, T), Optional propertyName As String = Nothing) As Boolean

Type Parameters

TModel

The type of model whose property (or field) to set.

T

The type of property (or field) to set.

Parameters

oldValue
T

The current property value.

newValue
T

The property's value after the change occurred.

model
TModel

The model containing the property being updated.

callback
Action<TModel,T>

The callback to invoke to set the target property value, if a change has occurred.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Remarks

The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.

Applies to

SetProperty<TModel,T>(T, T, IEqualityComparer<T>, TModel, Action<TModel,T>, String)

Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(T, T, String), with the difference being that this method is used to relay properties from a wrapped model in the current instance. See additional notes about this overload in SetProperty<TModel,T>(T, T, TModel, Action<TModel,T>, String).

protected bool SetProperty<TModel,T> (T oldValue, T newValue, System.Collections.Generic.IEqualityComparer<T> comparer, TModel model, Action<TModel,T> callback, string? propertyName = default) where TModel : class;
member this.SetProperty : 'T * 'T * System.Collections.Generic.IEqualityComparer<'T> * 'Model * Action<'Model, 'T (requires 'Model : null)> * string -> bool (requires 'Model : null)
Protected Function SetProperty(Of TModel As Class, T As Class) (oldValue As T, newValue As T, comparer As IEqualityComparer(Of T), model As TModel, callback As Action(Of TModel, T), Optional propertyName As String = Nothing) As Boolean

Type Parameters

TModel

The type of model whose property (or field) to set.

T

The type of property (or field) to set.

Parameters

oldValue
T

The current property value.

newValue
T

The property's value after the change occurred.

comparer
IEqualityComparer<T>

The IEqualityComparer<T> instance to use to compare the input values.

model
TModel

The model containing the property being updated.

callback
Action<TModel,T>

The callback to invoke to set the target property value, if a change has occurred.

propertyName
String

(optional) The name of the property that changed.

Returns

true if the property was changed, false otherwise.

Applies to