在silverlight中刷新DataGrid

本文关键字:DataGrid 刷新 silverlight | 更新日期: 2023-09-27 17:51:24

我有一个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]);
}

在silverlight中刷新DataGrid

每3秒刷新一次只需使用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

Scroll保持在DataGrid的相同位置:

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);