1

我正在尝试实现一种方法将数据网格的滚动条移回此示例之后的先前位置http://www.codeproject.com/Articles/109531/Controlling-and-Viewing-the-ScrollBar-Positions-的,但此方法始终返回 null,因此无法创建自动化。有谁知道为什么这总是返回null?

    public static IScrollProvider GetScrollProvider(DataGrid grid)
    {
        var p = FrameworkElementAutomationPeer.FromElement(grid)
            ?? FrameworkElementAutomationPeer.CreatePeerForElement(grid);
        return p.GetPattern(PatternInterface.Scroll) as IScrollProvider;
    }
4

2 回答 2

1

古代历史,但至少当前版本似乎在所有情况下都具有魅力。

//p.GetPattern(PatternInterface.Scroll) as IScrollProvider;
{System.Windows.Automation.Peers.DataGridAutomationPeer}
    [System.Windows.Automation.Peers.DataGridAutomationPeer]: 
        {System.Windows.Automation.Peers.DataGridAutomationPeer}
    HorizontallyScrollable: true
    HorizontalScrollPercent: 0.0
    HorizontalViewSize: 81.44329896907216
    VerticallyScrollable: true
    VerticalScrollPercent: 29.062733871459486
    VerticalViewSize: 2.9625
于 2014-09-29T16:23:54.043 回答
0

我知道它是一个旧帖子,但如果有人遇到同样的问题,(像我一样),这就是我解决它的方法:

出于某种原因,当您使用 DataGrid 的滚动条时,如果用户还没有滚动,它会通知最大位置为 0,即使它不是。所以我在更新之前保存了最大值并在之后恢复它:

首先,我创建一个类来保存这些值。它可能是变量,但一个类似乎更有条理:

public class ScrollInfo
{
    public double HorizontalPosition;
    public double HorizontalMaximum;
    public double VerticalPosition;
    public double VerticalMaximum;
}

在更新 ItemSource 属性之前,我使用此方法保存滚动信息:

public static ScrollInfo GetScrollInfo(this DataGrid grid)
{
    ScrollInfo oInfo = new ScrollInfo();

    ScrollBar sbHorizontal = grid.GetScrollbar(ScrollMode.Horizontal);
    oInfo.HorizontalMaximum = sbHorizontal.Maximum;
    oInfo.HorizontalPosition = sbHorizontal.Value;

    ScrollBar sbVertical = grid.GetScrollbar(ScrollMode.Vertical);
    oInfo.VerticalMaximum = sbVertical.Maximum;
    oInfo.VerticalPosition = sbVertical.Value;

    return oInfo;
}

然后我调用此方法在更新后将此信息设置回网格:

public static void SetScrollPosition(this DataGrid grid, ScrollInfo info)
{
    if (info.HorizontalPosition > 0)
    {
        ScrollBar sbHorizontal = grid.GetScrollbar(ScrollMode.Horizontal);
        sbHorizontal.Maximum = info.HorizontalMaximum;
        grid.Scroll(ScrollMode.Horizontal, info.HorizontalPosition);
    }

    if (info.VerticalPosition > 0)
    {
        ScrollBar sbVertical = grid.GetScrollbar(ScrollMode.Vertical);
        sbVertical.Maximum = info.VerticalMaximum;            
        grid.Scroll(ScrollMode.Vertical, info.VerticalPosition);
    }
}

我什至创建了一个 UpdateItemSource 方法来使事情变得更容易:

public static void UpdateItemSource(this DataGrid grid, IEnumerable itemSource)
{
    ScrollInfo oInfo = grid.GetScrollInfo();
    grid.ItemsSource = itemSource;
    grid.SetScrollPosition(oInfo);
}

我使用的其他方法,我想我从发布问题的用户所做的同一个帖子中获取:

public static void Scroll(this DataGrid grid, ScrollMode mode, double position)
{
    // Get the scrollbar and convert the position to percent.
    var scrollBar = grid.GetScrollbar(mode);
    double positionPct = ((position / scrollBar.Maximum) * 100);

    // Scroll to a specfic percentage of the scrollbar.
    grid.ScrollToPercent(mode, positionPct);
}

public static void ScrollToPercent(this DataGrid grid, ScrollMode mode, double percent)
{
    // Fix the percentage.
    if (percent < 0)
        percent = 0;
    else if (percent > 100)
        percent = 100;

    // Get the scroll provider.
    var scrollProvider = GetScrollProvider(grid);

    // Scroll.
    switch (mode)
    {
        case ScrollMode.Vertical:                        
            scrollProvider.SetScrollPercent( System.Windows.Automation.ScrollPatternIdentifiers.NoScroll, percent);
            break;
        case ScrollMode.Horizontal:
            scrollProvider.SetScrollPercent(percent, System.Windows.Automation.ScrollPatternIdentifiers.NoScroll);
            break;
    }
}
于 2016-04-19T17:21:39.237 回答