4

作为对这个问题的参考,我想知道如何将搜索到的项目滚动到网格视图的视图中

4

1 回答 1

6

您可以创建一个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"
于 2013-03-18T11:01:25.220 回答