找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 46|回复: 0

[wpf] 在 WPF 中,如果实时更新 GridView(或者称为 DataGrid)的数据

[复制链接] IP属地:广东省广州市白云区
发表于 2024-6-5 09:31:17 | 显示全部楼层 |阅读模式
在 WPF 中,如果想要实时更新 GridView(或者称为 DataGrid)的数据,可以考虑使用绑定的方式,并且在数据源发生变化时通知界面更新。以下是一个简单的示例代码,演示如何实时更新 GridView 的数据:
假设你有一个 ViewModel 如下:
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;

  3. public class MainViewModel : INotifyPropertyChanged
  4. {
  5.     public event PropertyChangedEventHandler PropertyChanged;

  6.     private ObservableCollection<MyData> _dataList;
  7.     public ObservableCollection<MyData> DataList
  8.     {
  9.         get { return _dataList; }
  10.         set
  11.         {
  12.             _dataList = value;
  13.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DataList));
  14.         }
  15.     }

  16.     public MainViewModel()
  17.     {
  18.         // 初始化数据
  19.         DataList = new ObservableCollection<MyData>
  20.         {
  21.             new MyData {Id = 1, Name = "Alice"},
  22.             new MyData {Id = 2, Name = "Bob"},
  23.             new MyData {Id = 3, Name = "Charlie"}
  24.         };
  25.     }

  26.     public void UpdateData()
  27.     {
  28.         // 模拟数据更新
  29.         DataList.Add(new MyData { Id = 4, Name = "David" });
  30.     }
  31. }
复制代码


创建一个简单的数据模型 MyData:
  1. public class MyData
  2. {
  3.     public int Id { get; set; }
  4.     public string Name { get; set; }
  5. }
复制代码

在你的 WPF 界面(例如 MainWindow.xaml),你可以使用 DataGrid 绑定到 ViewModel 中的 DataList,同时提供一个按钮来实时更新数据:
  1. <Window x:Class="YourNamespace.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:YourNamespace"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.     <Grid>
  10.         <Grid.RowDefinitions>
  11.             <RowDefinition Height="*"/>
  12.             <RowDefinition Height="Auto"/>
  13.         </Grid.RowDefinitions>

  14.         <DataGrid ItemsSource="{Binding DataList}" AutoGenerateColumns="True"/>
  15.         <Button Grid.Row="1" Content="Update Data" Click="UpdateData_Click"/>
  16.     </Grid>
  17. </Window>
复制代码

在 MainWindow.xaml.cs 中,确保设置窗口的 DataContext,并处理更新按钮的点击事件:

  1. public partial class MainWindow : Window
  2. {
  3.     public MainViewModel ViewModel { get; set; }

  4.     public MainWindow()
  5.     {
  6.         InitializeComponent();
  7.         
  8.         ViewModel = new MainViewModel();
  9.         DataContext = ViewModel;
  10.     }

  11.     private void UpdateData_Click(object sender, RoutedEventArgs e)
  12.     {
  13.         ViewModel.UpdateData();
  14.     }
  15. }
复制代码
以上代码演示了如何在 WPF 中实现实时更新 GridView 的数据。通过使用绑定和 ViewModel,可以很容易地更新数据源,并在界面上实时显示最新数据。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|西兴社区 ( 蜀ICP备2022005627号 )|网站地图

GMT+8, 2024-9-17 04:26 , Processed in 0.601508 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表