[英]How to create a dynamic WPF datagrid?
我有一個動態數據庫,數據庫中的數據將在一分鍾內更新。 現在,我在WPF項目中創建了一個數據網格,我想顯示數據庫中的所有數據。 當我運行我的項目時,datagrid將僅顯示靜態數據(項目運行之前的數據)。 我如何確保我的數據網格在運行后將保持自身更新 ? 順便說一句,我為WPF項目使用linq to sql和C#。
我的datagrid的代碼段:
<DataGrid AutoGenerateColumns="False" Name="MyDataGrid" VerticalAlignment="Top" Width="185" >
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Width="60" Binding="{Binding Date}" />
<DataGridTextColumn Header="Time" Width="55" Binding="{Binding Time}"/>
<DataGridTextColumn Header="Id" Width="69" Binding="{Binding id}" />
</DataGrid.Columns>
</DataGrid>
我的代碼背后的代碼段:
public MainWindow()
{
InitializeComponent();
using (MyDummyDataContext db = new MyDummyDataContext())
{
var query = from p in db.Ids
orderby p.Id descending
select new
{
Date = p.Date,
Time = p.Time,
id = p.Id
};
MyDataGrid.ItemsSource = query;
}
}
這是我的2美分:
這是在后台更新數據的鏈接,可能不完全適合您的問題,但是您可以得到一些建議:
編輯:
我只是寫了一個示例(為簡單起見,我使用DispatcherTimer來更新UI線程中的集合。要更新后台線程中的數據,您需要改用System.Timers.Timer,並在鏈接中使用方法。):
App.xaml.cs:
using System.Windows;
namespace DataGridTest
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var vm = new MainWindowViewModel();
var mainWindow = new MainWindow { DataContext = vm };
mainWindow.Show();
}
}
}
MainWindow.xaml
<Window x:Class="DataGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="False" Name="MyDataGrid" VerticalAlignment="Top" Width="185"
ItemsSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Width="60" Binding="{Binding Id}" />
<DataGridTextColumn Header="Name" Width="55" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
MainWindow.cs:
using System.Windows;
namespace DataGridTest
{
using System;
using System.ComponentModel;
using System.Windows.Threading;
public class Person : INotifyPropertyChanged
{
private int _id;
private string _name;
public int Id
{
get
{
return _id;
}
set
{
if (this._id == value)
return;
this._id = value;
this.OnPropertyChanged("Id");
}
}
public string Name
{
get
{
return _name;
}
set
{
if (this._name == value)
return;
this._name = value;
this.OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly DispatcherTimer _timer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += this._timer_Tick;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
var vm = this.DataContext as MainWindowViewModel;
if(vm != null)
vm.Refresh();
}
}
}
MainWindowViewModel.cs
namespace DataGridTest
{
using System.Collections.ObjectModel;
using System.ComponentModel;
public class MainWindowViewModel : INotifyPropertyChanged
{
private readonly ObservableCollection<Person> _persons = new ObservableCollection<Person>();
private static int _id = 3;
public ObservableCollection<Person> Persons
{
get
{
return _persons;
}
}
public MainWindowViewModel()
{
_persons.Add(new Person { Id = 1, Name = "A" });
_persons.Add(new Person { Id = 2, Name = "B" });
_persons.Add(new Person { Id = 3, Name = "C" });
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public void Refresh()
{
_persons.Add(new Person() { Id = ++_id, Name = _id.ToString() });
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.