我正在尝试在添加新项目时让RadDataGrid(UWP(自动滚动到底部。 我已经看到了以下内容:
- 绑定后滚动到辐射网格的最后一行?
- 自动滚动网格到底部
第二个很接近,但它没有提到更新 ItemsSource 时要绑定到的事件。
我在 C# 中使用它,在 Raspberry PI 上的 Windows 10 IoT 上使用它。
有没有一种简单的方法可以自动让它滚动?
我使用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; }
}