WPF ObservableCollection列表的属性变更通知方法

效果展示

WPF ObservableCollection列表的属性变更通知方法

cs代码

 1 using Microsoft.Toolkit.Mvvm.ComponentModel;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Collections.ObjectModel;
 5 using System.Collections.Specialized;
 6 using System.ComponentModel;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 
11 namespace MVVMToolkit框架学习.ViewModels
12 {
13     /// <summary>
14     /// 条目数据
15     /// </summary>
16     public class MaterialItem : ObservableObject
17     {
18         private double _price;
19 
20         public string Name { get; set; }
21 
22         public double Price
23         {
24             get => _price;
25             set => SetProperty(ref _price, value);
26         }
27 
28         public string Description { get; set; }
29     }
30 
31     public class ObservableCollectionVM : ObservableObject
32     {
33         private double _price;
34 
35         private ObservableCollection<MaterialItem> _materialItems;
36 
37         public ObservableCollection<MaterialItem> MaterialItems
38         {
39             get => _materialItems;
40             set => SetProperty(ref _materialItems, value);
41         }
42 
43         public double Price
44         {
45             get => _price;
46             set => SetProperty(ref _price, value);
47         }
48 
49         public ObservableCollectionVM()
50         {
51             MaterialItems = new ObservableCollection<MaterialItem>();
52             MaterialItems.CollectionChanged += (s, e) =>
53             {
54                 //监听元素变更事件,或者可以直接ForEach遍历所有的Item
55                 switch (e.Action)
56                 {
57                     case NotifyCollectionChangedAction.Add:
58                         foreach (var item in e.NewItems)
59                         {
60                             //挂载属性变更方法
61                             (item as INotifyPropertyChanged).PropertyChanged += (s, e) =>
62                             {
63                                 Price = MaterialItems.Sum(x => x.Price);
64                             };
65                         }
66                         break;
67 
68                     case NotifyCollectionChangedAction.Remove:
69                     case NotifyCollectionChangedAction.Replace:
70                     case NotifyCollectionChangedAction.Move:
71                     case NotifyCollectionChangedAction.Reset:
72                     default:
73                         break;
74                 }
75             };
76 
77             MaterialItems.Add(new MaterialItem() { Name = "玻璃" });
78             MaterialItems.Add(new MaterialItem() { Name = "大理石" });
79             MaterialItems.Add(new MaterialItem() { Name = "地板" });
80         }
81     }
82 }

xaml代码

 1 <Window
 2     x:Class="MVVMToolkit框架学习.Views.ObservableCollectionView"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:MVVMToolkit框架学习.Views"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     xmlns:vm="clr-namespace:MVVMToolkit框架学习.ViewModels"
 9     Title="ObservableCollectionView"
10     Width="800"
11     Height="450"
12     d:DataContext="{d:DesignInstance Type={x:Type vm:ObservableCollectionVM}}"
13     mc:Ignorable="d">
14     <Grid>
15         <Grid.RowDefinitions>
16             <RowDefinition Height="2*" />
17             <RowDefinition Height="8*" />
18         </Grid.RowDefinitions>
19         <StackPanel
20             HorizontalAlignment="Center"
21             VerticalAlignment="Center"
22             Orientation="Horizontal">
23             <TextBlock
24                 FontSize="25"
25                 Text="总价" />
26             <TextBlock
27                 Margin="15,0,0,0"
28                 FontSize="25"
29                 Text="{Binding Price, UpdateSourceTrigger=PropertyChanged}" />
30         </StackPanel>
31 
32         <DataGrid
33             Grid.Row="1"
34             HorizontalAlignment="Center"
35             ItemsSource="{Binding MaterialItems}" />
36     </Grid>
37 </Window>

 

 

上一篇:UI自动化


下一篇:2021.12.1