简体   繁体   English

使用 CommunityToolkit.Mvvm 处理可观察对象属性

[英]Handling observable object properties with CommunityToolkit.Mvvm

How do we handle properties that are objects themselves using CommunityToolkit.Mvvm ?我们如何使用CommunityToolkit.Mvvm处理本身就是对象的属性?

I understand that I can simply use [ObservableProperty] attribute with simple properties eg data type of string , int , etc.我知道我可以简单地使用具有简单属性的[ObservableProperty]属性,例如stringint等数据类型。

How do we handle properties that are POCO objects and need to be observable?我们如何处理POCO对象且需要可观察的属性?

Here's an example:这是一个例子:

public partial class MyViewModel : ObservableObject
{
   [ObservableProperty]
   string title;

   [ObservableProperty]
   decimal price;

   Person author; // How do we handle this property that is a Person object?
}

I understand that source generators in CommunityToolkit.Mvvm will automatically handle creation of Title and Price public properties.我了解CommunityToolkit.Mvvm中的源生成器将自动处理TitlePrice公共属性的创建。 What about author ? author呢?

Do we use [ObservableProperty] attribute to make complex properties observable, such as the author property in the above example which is a Person object?我们是否使用[ObservableProperty]属性来使复杂的属性可观察,例如上面示例中的author属性是一个Person对象?

Here are multiple scenarios:这里有多个场景:

The entire Person object gets replaced整个 Person 对象被替换

If you only ever replace the entire Person object and don't change its individual properties (you might select a different person from a list) all you need is to decorate the author field with the ObservablePropertyAttribute .如果您只替换整个 Person 对象并且不更改其各个属性(您可能会从列表中选择不同的人),那么您只需要使用ObservablePropertyAttribute装饰author字段。

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public partial class MyViewModel : ObservableObject
{
   [ObservableProperty]
   string title;

   [ObservableProperty]
   decimal price;

   [ObservableProperty]
   Person author;
}

This change will be reflected in the UI:此更改将反映在 UI 中:

Author = anotherPerson;

and this one will NOT:这个不会:

Author.FirstName = "James";

Individial Person properties get changed个人 Person 属性已更改

If you plan to change the persons individial properties, but keep the instance the same, you need to decorate the individual properties of the Person object with the ObservablePropertyAttribute .如果您打算更改 Person 的各个属性,但保持实例相同,则需要使用ObservablePropertyAttribute装饰Person对象的各个属性。

public class Person : ObservableObject
{
    [ObservableProperty]
    string firstName;

    [ObservableProperty]
    string lastName;
}

public partial class MyViewModel : ObservableObject
{
   [ObservableProperty]
   string title;

   [ObservableProperty]
   decimal price;

   public Person Author { get; } = new Person();
}

This change will be reflected in the UI:此更改将反映在 UI 中:

Author.FirstName = "James";

and this one will NOT:这个不会:

Author = anotherPerson;

Or both或两者

Or do both and get the UI to reflect any changes, be it changing an individual property or the entire object instance.或者两者都做,让 UI 反映任何更改,无论是更改单个属性还是整个对象实例。

public class Person : ObservableObject
{
    [ObservableProperty]
    string firstName;

    [ObservableProperty]
    string lastName;
}

public partial class MyViewModel : ObservableObject
{  
   [ObservableProperty]
   string title;

   [ObservableProperty]
   decimal price;

   [ObservableProperty]
   Person author;
}

Any of these changes will be reflected in the UI:这些更改中的任何一个都将反映在 UI 中:

Author = anotherPerson;
Author.FirstName = "James";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# CommunityToolkit.Mvvm ObservableProperty 列表 - C# CommunityToolkit.Mvvm ObservableProperty on a list 如何使用 CommunityToolkit.Mvvm 调用事件 - How to call events using CommunityToolkit.Mvvm 如何在 C# 中使用 Community - How do I bind an object that is periodically modified to a treeview in C# WPF using the CommunityToolkit.MVVM? CommunityToolkit.MVVM 命名空间“命名空间”已包含“类型”的定义 - CommunityToolkit.MVVM The namespace 'namespace' already contains a definition for 'type' 使用 CommunityToolkit.Mvvm 在 ObservableProperty 更改时调用方法 - Call method when ObservableProperty changes using CommunityToolkit.Mvvm 使用 CommunityToolkit.Mvvm 通知 CanExecuteChanged 的 RelayCommand 时出现 StackOverflow 异常 - StackOverflow Exception when notifying RelayCommand of CanExecuteChanged using CommunityToolkit.Mvvm 如何将 CommunityToolkit.Mvvm 中的源代码生成器用于 .NET 框架 4.7.2 WPF 应用程序 - How to use the source generators from CommunityToolkit.Mvvm for a .NET Framework 4.7.2 WPF Application 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 无法在使用 CommunityToolkit.Mvvm 的视图模型中使用 ICommand 属性 - Can't use ICommand attribute in view model using CommunityToolkit.Mvvm 当 ObservableCollection 中的 object 属性发生变化时,通知另一个属性 CommunityToolkit Mvvm - When object property in ObservableCollection changed, notify another property, CommunityToolkit Mvvm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM