简体   繁体   中英

How can I synchronize two scroll bars?

I want to sync two scroll bars; when the user up/down scroll2 auto set scroll1 with same postion. The scroll2 is an instance of an RichTextBox and scroll1 is an instance of ListView . I have no idea how do this.

Update

I think is closer now. Here is my current XAML code:

<RichTextBox ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Name="TextInput" AcceptsReturn="True" TextChanged="TextChangedEventHandler" Margin="0,23,0,9" ScrollViewer.ScrollChanged="OnScrollChangedHandler" HorizontalScrollBarVisibility="Visible" Grid.RowSpan="2" Grid.ColumnSpan="9" HorizontalAlignment="Right" Width="432.493">
            <RichTextBox.Resources>
                <Style TargetType="{x:Type Paragraph}">
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </RichTextBox.Resources>
        </RichTextBox>

    <ScrollViewer Name="ScrollRow" VerticalScrollBarVisibility="Visible" ScrollViewer.ScrollChanged="OnRowsScrollChangedHandler" ScrollViewer.HorizontalScrollBarVisibility="Visible" Grid.RowSpan="2" Margin="0,23,0,7.02" HorizontalAlignment="Left" Width="40">
                    <StackPanel Name="pScrollRow">
                      <ListView Margin="0,23,0,9" Name="Rows">
                        1.
                      </ListView>
          </StackPanel>
           </ScrollViewer>

C# code:

// ... 

 private void OnScrollChangedHandler(object sender, ScrollChangedEventArgs e)
        {
            ((IScrollInfo)pScrollRow).SetHorizontalOffset(e.HorizontalOffset);
        }

Add an event handler for ScrollBar.ValueChanged for both boxes, and have them change each other's ScrollBar.Value to the corresponding value. I haven't tested this, but seems reasonable! Good luck!

I would suggest you to add Element binding to sync the values, so you do not need to any code. Here's the details on MSDN about the element binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM