Silverlight DataGrid滚动条同步 [英] Silverlight DataGrid scrollbar synchronization

查看:78
本文介绍了Silverlight DataGrid滚动条同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Silverlight DataGrid,一个在另一个之上。我想同步他们的水平滚动条。
我试图将它们放在单独的scrollviewer中并将源scrollviewer的水平偏移设置为目标scrollviewer的水平偏移,但这不起作用,下面的DataGridscrollviewer分派器。我认为这可能是因为这些Datagrid在内部一个StackPanel?
我也尝试将这2个网格放在第三个网格中,并在其上应用scrollviewer,但这也不起作用

I have 2 Silverlight DataGrids one on top of another. I want to synchronize their horizontal scrollbars. I have tried to put them both in separate scrollviewers and set the horizontal offset of source scrollviewer to horizontal offset of target scrollviewer but that does not work, the below DataGrid scrollviewer disappers.I think that might be because these Datagrid are inside a StackPanel? I also tried to put these 2 grids in a third grid and apply scrollviewer on that but that does not work either

有人知道如何去吗对这个?
提前谢谢

Does anyone have an idea how to go about this? Thanks a lot in advance

推荐答案

我在SL4中做到了,不知道它是否在SL3中有效,对不起。文档指出该API已经存在,但我还没有尝试过。

I did this in SL4, have no idea if it works in SL3, sorry. The docs state that the API is there but I have not tried it.

诀窍是使用自动化对等体。获取两个网格的滚动模式自动化对等体。当在一个网格上滚动时,通过自动化对等方滚动另一个网格。

The trick is to use automation peers. Get the scroll pattern automation peers for both grids. When scrolling happens on one grid, scroll the other one through the automation peer.

为了使这一点更加具体,假设我们有2个网格,分别名为_dgGrowth和_dgTotals:

To make this more concrete, assuming we have 2 grids, named _dgGrowth and _dgTotals:

using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

    public partial class MyPageWithGrids : Page {
...
        private ScrollBar _sbGrowth, _sbTotals;
        private AutomationPeer _peerGrowth, _peerTotals;
        private bool _ignoreScrollEvents;

        private void OnPageLoaded(object sender, RoutedEventArgs e) {

            _sbGrowth = GetHorizontalScrollBar(_dgGrowth);
            if (_sbGrowth != null) {
                _sbGrowth.Scroll += OnScrollGrowthGrid;
            }
            _sbTotals = GetHorizontalScrollBar(_dgTotals);
            if (_sbTotals != null) {
                _sbTotals.Scroll += OnScrollTotalsGrid;
            }

            _peerGrowth = FrameworkElementAutomationPeer.CreatePeerForElement(_dgGrowth);
            _peerTotals = FrameworkElementAutomationPeer.CreatePeerForElement(_dgTotals);
        }

        private ScrollBar GetHorizontalScrollBar(DataGrid parentGrid) {
            return parentGrid.Descendents().OfType<ScrollBar>().FirstOrDefault(sb => sb.Name == "HorizontalScrollbar");
        }

        private void OnScrollTotalsGrid(object sender, ScrollEventArgs e) {

            if (! _ignoreScrollEvents) {
                SyncHorizontalScroll(_peerTotals, _peerGrowth);
            }
        }

        private void OnScrollGrowthGrid(object sender, ScrollEventArgs e) {

            if (! _ignoreScrollEvents) {
                SyncHorizontalScroll(_peerGrowth, _peerTotals);
            }
        }

        private void SyncHorizontalScroll(AutomationPeer source, AutomationPeer copy) {

            IScrollProvider sourceProvider = null;
            if (source != null) {
                sourceProvider = (IScrollProvider) source.GetPattern(PatternInterface.Scroll);
            }
            IScrollProvider copyProvider = null;
            if (copy != null) {
                copyProvider = (IScrollProvider) copy.GetPattern(PatternInterface.Scroll);
            }

            if (sourceProvider != null && copyProvider != null) {

                _ignoreScrollEvents = true;

                // scroll copy at horizontal position of source, and keep vertical position
                copyProvider.SetScrollPercent(sourceProvider.HorizontalScrollPercent, copyProvider.VerticalScrollPercent);

                _ignoreScrollEvents = false;
            }
        }
    }

未显示的设置将Loaded事件设置为OnPageLoaded,并在此问题中找到Descendants()方法

What is not shown is setting up the Loaded event to OnPageLoaded and the Descendants() method found in this question.

这篇关于Silverlight DataGrid滚动条同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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