imj
imj

Reputation: 470

Synchronized scrolling of two ScrollViewers with different content sizes

I have something like this:

Synchronized scrolling of two ScrollViewers whenever any one is scrolled in wpf

The problem is that the size of the content of the Scrollviewer do not have the same width.

I have managed to calculate the proportional movement of the bars but does not take into account the size of the bars, which are different from each other:

sv2.ScrollToHorizontalOffset(e.HorizontalOffset * (sv1.Width / grid1.Width) / (sv2.Width / grid2.Width));

sv1.ScrollToHorizontalOffset(e.HorizontalOffset * (sv2.Width / grid2.Width) / (sv1.Width / grid1.Width));

enter image description here

It only coordinates the start of the bars, regardless of their size. Any ideas?

Upvotes: 0

Views: 224

Answers (1)

Seididieci
Seididieci

Reputation: 404

You have to calculate the scrolling position in a fraction of the total:

After scrolloffset on sv1 changes try something like

var scFract = sv1.HorizontalOffset / sv1.ScrollableWith;
sv2.ScrollToHorizontalOffset(sv2.ScrollableWith * scFract);

Upvotes: 1

Related Questions