在 silverlight 中刷新 DataGrid
Refresh DataGrid in silverlight
我有 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]);
}
每 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);
我有 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]);
}
每 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);