gpt4 book ai didi

wpf - 更新数据源时刷新数据网格

转载 作者:行者123 更新时间:2023-12-04 14:33:16 26 4
gpt4 key购买 nike

我有一个显示绑定(bind)到 DataSource 的表格的数据网格。在时间限制上不断变化。
我的 DataSource 时如何刷新数据网格的内容值被更新。

P.S : 我的 DataSource 中的值表格由监控系统更新。其表值会定期更新。

我应该在我的 EF 哪里添加我的 Observable Collection?

    private IQueryable<MyTable> GetMyTablesQuery(TestDBEntities1 testDBEntities1 )
{
// definition of a Query to retrieve the info from my DB
System.Data.Objects.ObjectQuery<EF_demo1.MyTable> myTablesQuery = testDBEntities1.MyTables;
// Returns an ObjectQuery.
return myTablesQuery ;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
// A New entity container is created that uses the object context
var testDBEntities1 = new EF_demo1.HMDBEntities1();
// Load data into MyTables.
var myTablesViewSource= ((System.Windows.Data.CollectionViewSource)(this.FindResource("myTablesViewSource")));
// the query which is defined above is executed here. It grabs all the information from the entity container
IQueryable<EF_demo1.MyTable> myTablesQuery = this.GetMyTablesQuery(testDBEntities1 );
//The query result is binded to the source of the myTablesViewSource, which inturn binds back to the list.
myTablesViewSource.Source = myTablesQuery .ToList();
}

最佳答案

一种可能的方法是使用 ObservableCollection:

BoundCollection = new ObservableCollection<MyEntityType>(entities);

在哪里 BoundCollection用于您的绑定(bind)。然后每当更新值时,您将清除集合并重新添加它们:
BoundCollection.Clear();
foreach(var e in entities)
{
BoundCollection.Add(e);
}

这是另一个使用 INotifyPropertyChanged 并每次重新绑定(bind)集合的选项。但是,使用 ObservableCollection 是首选方法,因为它旨在添加和删除将自动更新 UI 的项目。
public class MyModel : INotifyPropertyChanged
{
public IList<MyEntity> BoundCollection {get; private set;}

public MyModel()
{
UpdateBinding();
}

private void UpdateBinding()
{
// get entities
BoundCollection = new List<MyEntity>(entities);
// this notifies the bound grid that the binding has changed
// and should refresh the data
NotifyPropertyChanged("UpdateBinding");
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged( string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

关于wpf - 更新数据源时刷新数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com