How can I synchronize two scroll bars?

淺唱寂寞╮ 提交于 2019-12-25 01:24:00

问题


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);
        }

回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/9028524/how-can-i-synchronize-two-scroll-bars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!