XAML 如何使用我自己的滚动条滚动ScrollViewer的内容?

7kjnsjlb  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(174)

我有一个滚动条和一个滚动浏览器。
我想使用自己的滚动条滚动ScrollViewer的内容。
这是我的代码:

<StackPanel HorizontalAlignment="Stretch">
    <ScrollBar
        x:Name="TestScrollBar"
        HorizontalAlignment="Stretch"
        Orientation="Horizontal"
        Scroll="TestScrollBar_Scroll"
        Maximum="{Binding ElementName=TestScrollViewer, Path=ExtentWidth, Mode=OneWay}"
        ViewportSize="{Binding ElementName=TestScrollViewer, Path=ViewportWidth, Mode=OneWay}"
        Visibility="Visible"
        Background="Aqua"/> <!--This is set for seeing when/if the ScrollBar is visible-->
    <ScrollViewer
        x:Name="TestScrollViewer"
        ViewChanged="TestScrollViewer_ViewChanged"
        HorizontalAlignment="Stretch"
        HorizontalScrollMode="Auto"
        HorizontalScrollBarVisibility="Auto"> <!--This is set to Auto for now, but should be Disabled/Hidden later-->
        <TextBlock FontSize="144" Text="This is blind text. Ignore this completely. It is only here to fill up the ScrollViewer horizontally."/>
    </ScrollViewer>
</StackPanel>
private void TestScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    TestScrollViewer.ChangeView(e.NewValue, null, null);
}

private void TestScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
    TestScrollBar.Value = TestScrollViewer.HorizontalOffset;
}

当我运行我的应用程序时,条形图在几毫秒内可见(浅绿色闪光),之后消失。
我使用Live可视树和Live属性资源管理器检查了XAML和代码中设置的所有值,但ScrollBar不可见,即使ScrollViewer的ScrollBar可见。

bwleehnv

bwleehnv1#

您只需要将IndicatorMode设置为MouseIndicator,默认情况下IndicatorModeNone
因此,您的ScrollBar代码应该如下所示:

<ScrollBar
    x:Name="TestScrollBar"
    HorizontalAlignment="Stretch"
    Background="Aqua"
    IndicatorMode="MouseIndicator"
    Maximum="{Binding ElementName=TestScrollViewer, Path=ExtentWidth, Mode=OneWay}"
    Orientation="Horizontal"
    Scroll="TestScrollBar_Scroll"
    ViewportSize="{Binding ElementName=TestScrollViewer, Path=ViewportWidth, Mode=OneWay}"
    Visibility="Visible" />

相关问题