Click here to Skip to main content
15,881,172 members
Articles / Silverlight
Tip/Trick

Synchronize ListBoxes easy

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Sep 2010CPOL 7.7K   1  
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:

C#
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:

XML
<ScrollViewer Padding="10" x:Name="scrollViewer0" Style="{StaticResource PageScrollViewerStyle}" Margin="10">
            <ListBox x:Name="SourceList" Background="#FFE8F8FF" />
        </ScrollViewer

>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Expediteapps
Spain Spain
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions

Deeply involved in Xamarin Forms LOB (including Azure Cloud with offline support, custom controls, dependencies) projects, WP8.1 & W10 projects, WPF modern styled projects. Portable libraries like portablePDF, portableOneDrive, portableReports and portablePrinting (using Google Printing API).


Web and apps showcase at:
Expediteapps


Take a look to my blog
Blog

Comments and Discussions

 
-- There are no messages in this forum --