视频网站文案,三亚高端服务网站,怎样查看网站的权重,韩国时尚网站欣赏文章目录 前言相关链接代码仓库项目配置代码初始代码ViewPersonViewModel 尝试老办法通知解决方案ObservableCollectionBindingListICollectionView 总结 前言
我们这次详细了解一下列表通知的底层是怎么实现的
相关链接 十月的寒流 MVVM实战技巧之#xff1a;可被观测的集合… 文章目录 前言相关链接代码仓库项目配置代码初始代码ViewPersonViewModel 尝试老办法通知解决方案ObservableCollectionBindingListICollectionView 总结 前言
我们这次详细了解一下列表通知的底层是怎么实现的
相关链接 十月的寒流 MVVM实战技巧之可被观测的集合ObservableCollection BindingList 代码仓库
我为了方便展示源代码我将代码提交到了代码仓库里面 B站【十月的寒流】对应课程的代码 Github仓库 项目配置
如何使用我这里就不展开说明了 WPF CommunityToolkit.Mvvm WPF CommunityToolkit.Mvvm Messenger通讯 WPF-UI HandyControl 简单介绍 WPF-UI HandyControl 控件简单实战IconPacks矢量图导入 Bogus.NET生成批量模拟数据
代码
初始代码
View UserControl.DataContextviewModels:DemoViewModel //UserControl.DataContextDockPanelStackPanel DockPanel.DockBottomButton Command{Binding AddItemCommand}Content添加数据/Button/StackPanelDataGrid ItemsSource{Binding People}/DataGrid/DockPanelPerson
public class Person
{public int Id { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public string FullName { get; set; }public DateOnly BirthDay { get; set; }public static Person FakerOne faker.Generate();public static IEnumerablePerson FakerMany(int count)faker.Generate(count);private static readonly FakerPerson faker new FakerPerson().RuleFor(tt.Id,ff.IndexFaker).RuleFor(tt.FirstName,ff.Name.FirstName()).RuleFor(tt.LastName,ff.Name.LastName()).RuleFor(tt.FullName,ff.Name.FullName()).RuleFor(tt.BirthDay,ff.Date.BetweenDateOnly(new DateOnly(1990,1,1),new DateOnly(2010,1,1)));
}ViewModel
public partial class DemoViewModel:ObservableObject
{[ObservableProperty]private ListModels.Person people new ListModels.Person();[RelayCommand]public void AddItem(){People.Add(Models.Person.FakerOne);}public DemoViewModel() {People Models.Person.FakerMany(5).ToList();}}现在的代码是没有实现通知点击按钮也不会添加 尝试老办法通知 public void AddItem(){People.Add(Models.Person.FakerOne);//没有效果//OnPropertyChanged(nameof(People));//没有效果//SetProperty(ref people, people);}而且在我们点击ListBox的时候会报错。这个就说明其实List已经修改了但是这个通知方法不行。原因是List指向的是一个地址空间这个地址空间并没有变化。
解决方案
ObservableCollection
简单的解决方案就是改成ObservableCollection这里就不展开说明了。 但是有一个问题这个ObservableCollection只在Count更新的时候触发自动更新。里面的Person值修改的时候是不会触发更新的。
如果有联动更新的需求可以直接在【CollectionChanged】添加对应的代码
BindingList
这里我就不展开说明了直接上视频的源代码了。 ICollectionView WPF 【十月的寒流】学习笔记(1):DataGrid过滤 更好的解决方案就是直接更新。我们直接刷新ItemSorce
UserControl x:ClassWpfMvvmDemo.Views.DemoViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:WpfMvvmDemo.Viewsxmlns:viewModelsclr-namespace:WpfMvvmDemo.ViewModelsmc:Ignorabledd:DesignHeight450d:DesignWidth800UserControl.DataContextviewModels:DemoViewModel //UserControl.DataContextDockPanelStackPanel DockPanel.DockBottomHorizontalAlignmentLeftOrientationHorizontalButton Command{Binding AddItemCommand}Margin5Content添加数据/ButtonButton Command{Binding UpIdCommand}Margin5Content增加Id/Button/StackPanelDataGrid ItemsSource{Binding PeopleView}/DataGrid/DockPanel
/UserControl
using Bogus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace WpfMvvmDemo.ViewModels
{public partial class DemoViewModel:ObservableObject{[ObservableProperty]private ListModels.Person people new ListModels.Person();[ObservableProperty]private ICollectionView peopleView;[RelayCommand]public void AddItem(){People.Add(Models.Person.FakerOne);//没有效果//OnPropertyChanged(nameof(People));//没有效果//SetProperty(ref people, people);//直接更新整个视图源PeopleView.Refresh();}[RelayCommand]public void UpId(){foreach (var item in People){item.Id 10;}PeopleView.Refresh();}public DemoViewModel() {People Models.Person.FakerMany(5).ToList();PeopleView CollectionViewSource.GetDefaultView(People);}}
}
为了方便我们也可以直接新建一个类这里就把代码放一下就不展开说明了 public class CollectionDataT where T : class{public IEnumerableT Data { get; set; }public ICollectionView CollectionView { get; set; }public CollectionData() { }public void Init(){CollectionView CollectionViewSource.GetDefaultView(Data);CollectionView.Refresh();}}总结
我觉得当时【十月的寒流】那个视频一直在想用MVVM去通知更新当然他的主题也是使用MVVM自动更新。但是ItemSorce随时都有可能发生修改。要么就是每次事件之后修改要么就给每个可能会触发的属性添加通知。