65.9K
CodeProject is changing. Read more.
Home

Synchronize ListBoxes easy

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 22, 2010

CPOL
viewsIcon

7856

An easy answer;

I find out an easy way for Silverlight 4 to synchronize two ListBox or any that has a ScrollContentPresenter I started with silverlightdatagridscroll article I modified the method GetScrollBar to this:
public ScrollContentPresenter GetScrollPresenter(DependencyObject dep)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
            {
                var child = VisualTreeHelper.GetChild(dep, i);
                if (child != null && child is ScrollContentPresenter)
                    return child as ScrollContentPresenter;
                else
                {
                    ScrollContentPresenter sub = GetScrollPresenter(child);
                    if (sub != null)
                        return sub;
                }
            }
            return null;
        }
Now, I implemented the following in my code:
GetScrollPresenter(scrollViewer0).LayoutUpdated += (s1, e1) =>
{
  try
     {
        if(GetScrollPresenter(scrollViewer1)!=null && GetScrollPresenter(scrollViewer0) !=null)
                            GetScrollPresenter(scrollViewer1).SetVerticalOffset(GetScrollPresenter(scrollViewer0).VerticalOffset);
     }
     catch
     {
     }
};
And the last thing, in my case I have the following in the XAML:
<ScrollViewer Padding="10" x:Name="scrollViewer0" Style="{StaticResource PageScrollViewerStyle}" Margin="10">
            <ListBox x:Name="SourceList" Background="#FFE8F8FF" />
        </ScrollViewer
>