ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-28 20:34:45  阅读:404  来源: 互联网

标签:case ObservableCollection price MaterialItems System 列表 using WPF public


效果展示

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>

 

 

标签:case,ObservableCollection,price,MaterialItems,System,列表,using,WPF,public
来源: https://www.cnblogs.com/AtTheMoment/p/15742742.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有