WPF滚动条控制其他ScrollViews

时间:2014-04-15 17:23:21

标签: wpf scrollbar scrollview

我有几个带有隐藏垂直和水平滚动条的ScrollView。我希望屏幕上的滚动条不属于任何ScrollViews,它应该能够控制(同步)所有ScrollViews的滚动。换句话说,当我滚动"主人"滚动条我可以让所有ScrollViews滚动相同的偏移量。可能吗?如果是,我该如何实现?提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是解决方案的示例。

背后的代码

public MainWindow()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ((FrameworkElement)svContainer1.Content).SizeChanged += new SizeChangedEventHandler(content_SizeChanged);

    sbScroller.Minimum = 0;
    sbScroller.Maximum = svContainer1.ExtentHeight - svContainer1.ViewportHeight;
}

void content_SizeChanged(object sender, SizeChangedEventArgs e)
{
    sbScroller.Minimum = 0;
    sbScroller.Maximum = svContainer1.ExtentHeight - svContainer1.ViewportHeight;
}

//ScrollBar event handler
private void ScrollBar_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
    UpdateScrollOffset(sbScroller.Value);
}

//Updates the offset of both ScrollViewer
public void UpdateScrollOffset(double newOffset)
{
    svContainer1.ScrollToVerticalOffset(newOffset);
    svContainer2.ScrollToVerticalOffset(newOffset);
}

//Event handler for all ScrollViewers
private void Grid_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    UpdateScrollOffset(e.VerticalOffset);
}

Xaml代码:

<Grid ScrollViewer.ScrollChanged="Grid_ScrollChanged">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.5*"/>
        <ColumnDefinition Width="0.5*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>


    <ScrollViewer x:Name="svContainer1"  VerticalScrollBarVisibility="Hidden" Grid.Column="0">
        <Grid Height="1000">
            <Grid.RowDefinitions>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
            </Grid.RowDefinitions>

            <Rectangle Fill="Red" Grid.Row="0"/>
            <Rectangle Fill="Green" Grid.Row="1"/>
            <Rectangle Fill="Violet" Grid.Row="2"/>
            <Rectangle Fill="Blue" Grid.Row="3"/>

        </Grid>
    </ScrollViewer>

    <ScrollViewer x:Name="svContainer2" VerticalScrollBarVisibility="Hidden" Grid.Column="1">
        <Grid Height="1000">
            <Grid.RowDefinitions>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
                <RowDefinition Height=".25*"/>
            </Grid.RowDefinitions>

            <Rectangle Fill="Red" Grid.Row="0"/>
            <Rectangle Fill="Green" Grid.Row="1"/>
            <Rectangle Fill="Violet" Grid.Row="2"/>
            <Rectangle Fill="Blue" Grid.Row="3"/>

        </Grid>
    </ScrollViewer>

    <ScrollBar x:Name="sbScroller" Grid.Column="3" Minimum="0" Scroll="ScrollBar_Scroll"  Maximum="100"/>
</Grid>

实际上没有简单的方法。您必须侦听ScrollBar事件并更新ScrollViewers的偏移量。您还必须收听ScrollViewer的内容大小更改,以便更新ScrollBar的滚动边界。