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



}
相关推荐
大霸王龙12 小时前
系统模块与功能设计框架
人工智能·wpf
明耀1 天前
WPF DataGrid 默认显示行号
wpf
lph19721 天前
wpf的converter
wpf
fyifei05581 天前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友1 天前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf20231 天前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白1 天前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans2 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf