在 Silverlight 中刷新 DataGrid
Posted
技术标签:
【中文标题】在 Silverlight 中刷新 DataGrid【英文标题】:Refresh DataGrid in silverlight 【发布时间】:2016-10-30 10:52:44 【问题描述】:我有 DataGrid
里面有列。
我希望DataGrid
每 3 秒刷新一次,其中包含来自服务的数据。
我需要Scroll
每次刷新都不会跳到DataGrid
的顶部,并保持在Scroll
的同一位置。有可能吗?
这是我的代码:
<sdk:DataGrid Grid.Row="1" FlowDirection="RightToLeft" ColumnHeaderHeight="32"
AutoGenerateColumns="False" Name="RealTimeReportDataGrid"
RowEditEnded="LinesDataGrid_RowEditEnded" RowHeight="40">
<sdk:DataGrid.Columns>...</sdk:DataGrid.Columns>
</sdk:DataGrid>
这是后面的代码:
public RealTimeReport()
InitializeComponent();
Loaded += OnViewLoaded;
RealTimeReportService.RealTimeReportServiceClient RTws = new RealTimeReportService.RealTimeReportServiceClient();
RTws.GetRealTimeReportAsync();
RTws.GetRealTimeReportCompleted += new EventHandler<RealTimeReportService.GetRealTimeReportCompletedEventArgs>(RTws_GetRealTimeReportCompleted);
void RTws_GetRealTimeReportCompleted(object sender, RealTimeReportService.GetRealTimeReportCompletedEventArgs e)
var t = e.Result.ToList();
RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
private void OnViewLoaded(object sender, RoutedEventArgs e)
Start();
private void OnUpdaterTimerTick(object sender, EventArgs e)
RealTimeReportService.RealTimeReportServiceClient RTws = new RealTimeReportService.RealTimeReportServiceClient();
RTws.GetRealTimeReportAsync();
RTws.GetRealTimeReportCompleted += new EventHandler<RealTimeReportService.GetRealTimeReportCompletedEventArgs>(RTws_GetRealTimeReportCompleted);
public void Start()
InitializeRefreshDataTimer();
public void InitializeRefreshDataTimer()
_updaterTimer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
_updaterTimer.Tick += OnUpdaterTimerTick;
_updaterTimer.Start();
先谢谢了。
更新: @StepUp,这是我做的唯一改变,根据你说的,但仍然没有改变:(
void RTws_GetRealTimeReportCompleted(object sender, RealTimeReportService.GetRealTimeReportCompletedEventArgs e)
var t = e.Result.ToList();
RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
this.RealTimeReportDataGrid.Dispatcher.BeginInvoke(() =>
RealTimeReportDataGrid.ScrollIntoView(
RealTimeReportDataGrid.SelectedItem,
RealTimeReportDataGrid.CurrentColumn);
);
//RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
//RealTimeReportDataGrid.UpdateLayout();
//RealTimeReportDataGrid.ScrollIntoView(RealTimeReportDataGrid.SelectedItem, RealTimeReportDataGrid.Columns[0]);
【问题讨论】:
【参考方案1】:每 3 秒刷新一次,只需 use Timer
class:
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=3000;
aTimer.Enabled=true;
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
//add new data
Elapsed
事件将每 X 秒引发一次,以毫秒为单位由 Timer 对象的 Interval
属性指定。它将调用您指定的Event Handler
方法,在上面的示例中为OnTimedEvent
。
在DataGrid
中保持Scroll
的相同位置:
yourDataGrid.ItemsSource = data;
yourDataGrid.UpdateLayout();
yourDataGrid.ScrollIntoView(theDataGrid.SelectedItem, theDataGrid.Columns[0]);
更新:
yourDataGrid.Dispatcher.BeginInvoke(() =>
yourDataGrid.Focus();
yourDataGrid.SelectedItem=whateverYouWant;
yourDataGrid.CurrentColumn=yourDataGrid.Columns[0];
yourDataGrid.ScrollIntoView(yourDataGrid.SelectedItem, yourDataGrid.CurrentColumn);
);
或者:
yourDataGrid.SelectedIndex = 5; // the index you want to select
yourDataGrid.UpdateLayout();
yourDataGrid.ScrollIntoView(Grid.SelectedItem, 0);
【讨论】:
我编辑了我的原始帖子,就在您回复的同时。现在刷新没问题,但是我向下滚动后,滚动仍然跳到顶部。 @David 你看到我答案的底部了吗?你试过yourDataGrid.ScrollIntoView(theDataGrid.SelectedItem, theDataGrid.Columns[0]);
吗?
是的,我试图将它放在“RTws_GetRealTimeReportCompleted”的末尾。但什么都没有改变。
@David 随时提出任何问题。如果您觉得我的回复对您有帮助,那么您可以将我的回复标记为答案,以简化以后其他人的搜索。请阅读此meta.stackexchange.com/questions/5234/…
谢谢,对我有帮助!以上是关于在 Silverlight 中刷新 DataGrid的主要内容,如果未能解决你的问题,请参考以下文章