WPF ListView实时更新数据

csharp 复制代码
<Window x:Class="TestListView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView 
            Width="300"
            Height="500" 
            BorderBrush="Black"
            BorderThickness="1"
            Name="UserList">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="状态名" Width="200"
                                   DisplayMemberBinding="{Binding NameLt}"></GridViewColumn>
                    <GridViewColumn Header="状态"  Width="100"
                                   DisplayMemberBinding="{Binding StatueLt}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Width="200" Height="40"   Margin="590,150,3.333,230.667" Click="Button_Click" > 点击一下开启线程</Button>
    </Grid>
</Window>
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestListView
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private static ObservableCollection<UserData> List1 = new ObservableCollection<UserData>();

        public MainWindow()
        {
            InitializeComponent();
            UserList.ItemsSource = List1;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    UserList.Dispatcher.Invoke(new Action(() => {
                        List1 = UserData.GettestDate();
                        UserList.ItemsSource = List1;
                    }));
           
                    Thread.Sleep(2000);
                }
            });
        }
    }



    class UserData : INotifyPropertyChanged //通知接口
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _NameLt;
        private bool _StatueLt;
        public string NameLt
        {
            get { return _NameLt; }
            set
            {
                _NameLt = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NameLt"));
            }
        }
        public bool StatueLt
        {
            get { return _StatueLt; }
            set
            {
                _StatueLt = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("StatueLt"));
            }
        }
        public UserData()
        {
            _StatueLt = StatueLt;
            _NameLt = NameLt;
        }



        public static ObservableCollection<UserData> GettestDate()
        {
            ObservableCollection<UserData> oneSources = new ObservableCollection<UserData>();

            for (int i = 0; i < 5; i++)
            {
                var random = new Random();
                oneSources.Add(new UserData() { NameLt = DateTime.Now.ToString(), StatueLt = random.NextDouble() >= 0.5 });

                Thread.Sleep(200);
            }
         return  oneSources;
        }
    }



}
相关推荐
狮恒1 小时前
OpenHarmony Flutter 分布式任务调度:跨设备资源协同与负载均衡方案
分布式·flutter·wpf·openharmony
嗝o゚2 小时前
Flutter适配鸿蒙多屏异构UI开发实战
flutter·开源·wpf·harmonyos
小白|2 小时前
Flutter 与 OpenHarmony 深度集成:实现跨设备传感器数据协同监测系统
flutter·wpf
棉晗榜2 小时前
WPF输入框里面加文本提示
wpf
嗝o゚3 小时前
鸿蒙跨端协同与Flutter结合的远程办公轻应用开发
flutter·华为·wpf
豫狮恒3 小时前
OpenHarmony Flutter 分布式权限管理:跨设备可信访问与权限协同方案
分布式·flutter·wpf·openharmony
小白|3 小时前
Flutter 与 OpenHarmony 深度整合:构建跨设备统一剪贴板同步系统
flutter·wpf
He BianGu3 小时前
【笔记】在WPF中如何使用ContentPresenter 与 Generic.xaml 设置数据默认 DataTemplate
windows·笔记·wpf
小白|4 小时前
Flutter 与 OpenHarmony 深度融合:实现分布式文件共享与跨设备协同编辑系统
分布式·flutter·wpf
豫狮恒4 小时前
OpenHarmony Flutter 分布式数据持久化:跨设备数据一致性与同步方案
分布式·安全·flutter·wpf·openharmony