XAML ScrollBar不可见,尽管其属性正确

nkcskrwz  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(145)

我有一个滚动条和一个滚动浏览器。
我想使用自己的滚动条滚动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 Visual Tree和Live Property Explorer检查了XAML和代码中设置的所有值,一切似乎都很好,但ScrollBar不可见,即使ScrollViewer的ScrollBar是可见的。
我该怎么补救呢?
就连ChatGPT都说我应该在StackOverflow上问。

wlwcrazw

wlwcrazw1#

您只需要将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" />

相关问题