我如何可以滚动搜索的项目为GridView中的WPF的观点 [英] How can I scroll the searched item into the view of GridView in Wpf

查看:119
本文介绍了我如何可以滚动搜索的项目为GridView中的WPF的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为参考<一href="http://stackoverflow.com/questions/15467553/proper-datagrid-serach-from-textbox-in-wpf-using-mvvm">this问题我想知道我怎么可以滚动搜索的项目进网格视图视图

As a reference to this question I want to know how can I scroll the searched item into the view of grid view

推荐答案

您可以创建一个 AttachedProperty 来跟踪 SelectedTtem ,如果需要滚动它进入视野。

You can create an AttachedProperty to keep track of the SelectedTtem and Scroll it into view if needed.

   // 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"

这篇关于我如何可以滚动搜索的项目为GridView中的WPF的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆