WFP Listbox绑定数据后,数据变化的刷新

Listbox绑定数据通过ItemsSource来的,如果绑定的是普通的List<数据>,不会自己刷新。

使用ObservableCollection集合

解决问题的方法:

  1. 将数组替换为 ObservableCollection
    ObservableCollection 是专为绑定设计的集合类型,可以通知 WPF 绑定的集合数据发生变化。

如果本身是List,可以修改为:

csharp 复制代码
using System.Collections.ObjectModel;

public ObservableCollection<MyItem> MyItems { get; set; }

public MainViewModel()
{
    MyItems = new ObservableCollection<MyItem>
    {
        new MyItem { showsafeui = true, SomeOtherProperty = "Item 1" },
        new MyItem { showsafeui = false, SomeOtherProperty = "Item 2" },
        new MyItem { showsafeui = true, SomeOtherProperty = "Item 3" }
    };
}

// 动态添加或删除数据
public void AddNewItem()
{
    MyItems.Add(new MyItem { showsafeui = true, SomeOtherProperty = "New Item" });
}

public void RemoveItem()
{
    if (MyItems.Any())
        MyItems.RemoveAt(0);
}

使用 ObservableCollection 后,新增、删除、或重新排序数据时,ListBox 会自动刷新。

数据内容变化的刷新INotifyPropertyChanged

上面的数据,如果某个属性的内容发生了变化如何刷新。可以使用INotifyPropertyChanged。

csharp 复制代码
public class HeartInfo : INotifyPropertyChanged
{
    public int id { get; set; }
    public bool showsafeui{ get; set; }
    
	public event PropertyChangedEventHandler PropertyChanged;
	protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
	{
	    Console.WriteLine("刷新UI------------");
	    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
	}
}

刷新单个数据

这样在某个属性变化的时候,可以

csharp 复制代码
	public bool showsafeui
    {
        get => _showsafeui;
        set
        {
            if (_showsafeui != value)
            {
                _showsafeui = value;
                OnPropertyChanged(); // 通知绑定属性更改
            }
        }
    }

刷新多个数据

如果某个属性变化需要刷新多个

csharp 复制代码
	private bool _showsafe;

    public bool showsafe
    {
        get => _showsafe;
        set
        {
            if (_showsafe != value)
            {
                _showsafe = value;
                OnPropertyChanged(); // 通知绑定的 showsafe 属性
                OnPropertyChanged(nameof(showsafeui)); // 通知绑定的 showsafeui 属性
            }
        }
    }

简化刷新所有

我每次都想有一个属性变化,刷新所有属性

可以OnPropertyChanged(string.Empty)传入空

csharp 复制代码
	private int _updateall  = 0;
	public int updateall 
	{
    	get { 
    	return _updateuistat;
    }
    set {
        if (_updateall!= value) { 
            _updateall= 0; 
            OnPropertyChanged(string.Empty); 
        }
    }  //刷新ui
}

这样我每次属性变化都调用updateall = 1,就全部刷新了。

相关推荐
DreamLife☼6 小时前
复杂Agent系统架构设计
系统架构·wpf·agent·组件·设计·构架·说明
SamChan9012 小时前
PDF翻译服务端到端基准测试:4款主流方案的吞吐量、延迟、内存占用对比
服务器·网络·python·ai·pdf·wpf
bugcome_com12 小时前
Avalonia 控件模板实战:从 WPF 迁移自定义 Button 样式的完整指南
wpf
七夜zippoe12 小时前
DolphinDB 故障排查实战:系统、数据库与集群常见问题诊断
数据库·wpf·集群·常见问题·故障排查·dolphindb
dalong102 天前
WPF:箭头绘制程序
wpf·自定义绘制
张工在路上2 天前
WPF 布局基础概述
大数据·hadoop·wpf
迪飞特科技3 天前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174176 天前
Redis 简介
数据库·redis·wpf
SamChan907 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan907 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf