在WPF中我想要datagrid内容实时滚动需要做?如下图所示
如果界面中不用定时器或者线程 内容是可以滚动,定时器和this.Dispatcher.BeginInvoke就没有滚动效果呢?
代码如下
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// var timer = new System.Timers.Timer();
// timer.Interval = 10000; //1m触发一次
// timer.Start();
// timer.Elapsed += (o, args) =>
//{
try
{
int Cont = 0;
ObservableCollection<object> WarningList = new ObservableCollection<object>();
AreaDepInOutRecordViewModel Model = new AreaDepInOutRecordViewModel();
IList<AreaDepInOutRecordModel> areaDepInOutRecordModels = Model.GetAreaDepInOutRecord(ConfigurationManager.AppSettings["DeptNameID"].ToString(), out Cont);
foreach (AreaDepInOutRecordModel item in areaDepInOutRecordModels)
{
WarningList.Add(new AreaDepInOutRecordModel()
{
DepartmentName = item.DepartmentName,
Count = item.Count,
});
}
//this.Dispatcher.BeginInvoke(new Action(delegate
// {
this.DataContext = WarningList;
this.sumCount.Content = "当前总人数:" + Cont;
//}));
}
catch (Exception ex)
{
}
//};
}
async void LoadDataToViewAsync() { await Task.Run(() => LoadDataToView()); } private bool _isLoading; void LoadDataToView() { if(_isLoading)return; _isLoading = true; try { var db = new lgameEntities(); var collection = new ObservableCollection<BindingNotify>(db.shop.ToList().Select(t => new BindingNotify(t))); Dispatcher.Invoke(() =>{_cGrid.ItemsSource = collection; }); } catch (Exception e) { MessageBox.Show(e.Message); } _isLoading = false; }
绑定属性没对=》ItemsSource。上面给你个参考
谢谢分享不是我想要的
@友情岁月: 真不知道就一个更新有什么复杂。集合用ObservableCollection,对象实现INotifyPropertyChanged,然后绑定起来,那不管你是变更一个还是整体,只要数据变界面就一定会变。
BindingNotify是个实现了INotifyPropertyChanged通过类而已,换成你的类,加个线程或者Timer去修改整体或者单个Item就搞定了。
什么叫实时滚动?请题主指导下。
就是dataGird里面内容实时滚动,由下往上滚动