作为对this问题的引用,我想知道如何将搜索到的项目滚动到网格视图的视图中
答案 0 :(得分:4)
您可以创建AttachedProperty
来跟踪SelectedTtem
并在需要时将其滚动到视图中。
// Using a DependencyProperty as the backing store for AutoScrollToSelectedRow. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AutoScrollToSelectedRowProperty =
DependencyProperty.RegisterAttached("AutoScrollToSelectedRow", typeof(bool), typeof(DataGridTextSearch)
, new UIPropertyMetadata(false, OnAutoScrollToSelectedRowChanged));
public static bool GetAutoScrollToSelectedRow(DependencyObject obj)
{
return (bool)obj.GetValue(AutoScrollToSelectedRowProperty);
}
public static void SetAutoScrollToSelectedRow(DependencyObject obj, bool value)
{
obj.SetValue(AutoScrollToSelectedRowProperty, value);
}
public static void OnAutoScrollToSelectedRowChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
{
var datagrid = s as DataGrid;
if (datagrid != null)
{
datagrid.IsSynchronizedWithCurrentItem = true;
datagrid.EnableRowVirtualization = !((bool)e.NewValue);
datagrid.SelectionChanged += (g, a) =>
{
if (datagrid.SelectedItem != null)
{
datagrid.ScrollIntoView(datagrid.SelectedItem);
}
};
}
}
用法:
<DataGrid local:DataGridTextSearch.AutoScrollToSelectedRow="True"