如何使用自定义的滚动条滚动 ScrollViewer 的内容?

huangapple go评论65阅读模式
英文:

How to scroll the contents of the ScrollViewer using my own ScrollBar?

问题

我有一个ScrollBar和一个ScrollViewer。
我想使用自己的ScrollBar滚动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;
}

当我运行我的应用程序时,ScrollBar在运行后可见几毫秒(呈现为水绿色的闪烁),然后消失。
我使用Live Visual Tree和Live Property Explorer检查了在XAML和我的代码中设置的所有值,但是ScrollBar不可见,即使ScrollViewer中的ScrollBar也不可见。

英文:

I have a ScrollBar and a ScrollViewer.
I want to scroll the contents of the ScrollViewer using my own ScrollBar.

This is my code:

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

When I run my app, the Bar is visible for a few milliseconds (a aqua-colored flash) and disappears after.
I checked all the values that are set in XAML and my code using the Live Visual Tree and Live Property Explorer, but the ScrollBar isn't visible, even when the one of the ScrollViewer is.

答案1

得分: 0

你只需要将IndicatorMode设置为MouseIndicatorIndicatorMode 默认值为None

所以,你的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" />
英文:

You just need to set IndicatorMode to MouseIndicator. The IndicatorMode is None by default.

So, your ScrollBar code should be like this:

<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" />

huangapple
  • 本文由 发表于 2023年2月8日 19:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定