Telerik for UWP RadDataGrid - 添加项目时滚动到底部

时间:2017-11-20 17:53:02

标签: c# uwp telerik

我想在添加新项目时将RadDataGrid(UWP)自动滚动到底部。我已经看到以下内容:

  1. scroll to last row of RadGrid after binding?
  2. Autoscroll grid to the bottom
  3. 第二个是关闭的,但它没有提到要在更新ItemsSource时绑定的事件。

    我在使用Raspberry PI的Windows 10 IoT的c#中使用它。

    有一种简单的方法可以让它自动滚动吗?

1 个答案:

答案 0 :(得分:0)

我使用ObservableCollection对象作为RadDataGrid的ItemsSource。然后,我注册CollectionChanged事件以检测ItemsSource是否更新。在事件处理程序中,您可以调用DataGrid.ScrollIndexIntoView方法滚动到底部。

请参阅我的以下代码示例以供参考:

<telerikGrid:RadDataGrid x:Name="DataGrid"/>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        list = new ObservableCollection<Data>{
             new Data { Country = "India", Capital = "New Delhi"},
             new Data { Country = "South Africa", Capital = "Cape Town"},
             new Data { Country = "Nigeria", Capital = "Abuja" },
             new Data { Country = "Singapore", Capital = "Singapore" },
             new Data { Country = "India", Capital = "New Delhi"}
        };
        list.CollectionChanged += List_CollectionChanged;
        DataGrid.ItemsSource = list;
    }

    private void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            DataGrid.ScrollIndexIntoView(list.Count - 1);
        }
    }
    public ObservableCollection<Data> list { get; set; }
}

public class Data
{
    public string Country { get; set; }

    public string Capital { get; set; }
}