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,就全部刷新了。

相关推荐
necessary65320 小时前
从工行“余额归零”事件看CAP定理:当金融系统在一致性与可用性之间做出选择
分布式·金融·wpf·可用性测试
棉晗榜21 小时前
WPF隐藏控件后,怎么让其上部的控件空间自动撑高
wpf
壹佰大多2 天前
【Redisson分布式锁源码分析-3】
数据结构·分布式·mysql·spring·spring cloud·wpf·lua
LateFrames2 天前
以小白视角尝试 WPF / WinUI3 / MAUI / MAUI Blazor 构建 Windows 桌面程序
windows·wpf·maui·mauiblazor·winui3
偶尔的鼠标人3 天前
Avalonia/WPF 打开子窗口,并且跨页面传值
c#·wpf·mvvm·avalonia
玖笙&3 天前
✨WPF编程进阶【6.1】:图形原则(附源码)
c++·c#·wpf·visual studio
lixy5793 天前
WPF检测网络状态切换
wpf
纸照片3 天前
WPF中为Button设置IsMouseOver和IsPressed事件中改变背景颜色不起作用
c#·.net·wpf
Aevget3 天前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(四)
ui·.net·wpf·devexpress·wpf控件
Z_W_H_3 天前
【ArcGISProSDK】刷新按钮样式
wpf·arcgisprosdk