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;
        }
    }



}
相关推荐
code_shenbing2 小时前
WPF实现打印机控制及打印
wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪1 天前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮2 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥3 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo3 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown4 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔5 天前
WPF8-常用控件
wpf