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

相关推荐
吾与谁归in4 小时前
C#脚本工具,支持智能提示自动补全(WPF + Roslyn + AvalonEdit)
开发语言·c#·wpf
昂好的day4 小时前
WPF 从零入门(上):WPF 是什么、XAML 语法、布局与样式全解
开发语言·c#·wpf
影寂ldy1 天前
WPF导航窗体+Page页面跳转 知识点整理
wpf
He BianGu1 天前
【WPF-Control教程】IOC 依赖注入
c#·wpf·wpf-control
He BianGu1 天前
【WPF-Control教程】H.Extensions.Behvaiors 开发文档
c#·wpf·wpf-control
He BianGu1 天前
【WPF-Control教程】H.Style 开发文档
c#·wpf·wpf-control
麻瓜code1 天前
分布式锁演进:从一个用户下两单,到 Redisson,我改了四版
分布式·wpf
He BianGu1 天前
【WPF-Control教程】MVVM 二次开发文档
c#·wpf·wpf-control
He BianGu1 天前
【WPF-Control教程】Theme 主题系统二次开发文档
c#·wpf·wpf-control
小羊没烦恼!4 天前
分布式事务 | 使用DTM 的Saga 模式
java·linux·运维·数据库·wpf