Should the movement of the scroll bar of the First Listbox affect the movement of the scroll bar of the Second Listbox? But how?

I have 4 lists in my WPF application. Each of them, at any given moment in time, contains no equal. String ListBoxItems in them. If the selected index of any of them changes, the other three also reflect the same behavior. I want that when the user moves the scroll bar of one of them, the other three should also move at the same time.

I tried Scrollintoview, but it does not work bcoz, if I select the listBox element and apply scrollintoview to the other three lists, the element selected in them comes from the top.

That is why I believe that moving the scrollbar should be the best option for this. How to do it?

+1
source share
2 answers

In XAML, hook the ScrollChanged event

ScrollViewer.ScrollChanged="ListBox_ScrollChanged"

In CodeBehind, find Scrollviewers inside ListBoxes and apply vertical offset:

    private void ListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
      var sourceScrollViewer = FindVisualChild<ScrollViewer>(sender as DependencyObject) as ScrollViewer;
      var targetScrollViewer = FindVisualChild<ScrollViewer>(listBox2) as ScrollViewer;
      targetScrollViewer.ScrollToVerticalOffset(sourceScrollViewer.VerticalOffset);
    }

// helper method
    private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
    {
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
      {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is childItem)
          return (childItem)child;
        else
        {
          childItem childOfChild = FindVisualChild<childItem>(child);
          if (childOfChild != null)
            return childOfChild;
        }
      }
      return null;
    }
+2
source

Well in the code, it's something like this:

1) get four scrollviewers from four lists of ListViews (by searching them inside a child (VisualTreeHelper.getchild) inside a method such as FindDescendant (...))

2) bind their scroll events (ScrollChanged) to a common subitem, get a VerticalOffset of what caused the event and ScrollToVerticalOffset (.) Others.

should be possible in haml, but it seems to me more complicated.

0
source

All Articles