英文:
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
设置为MouseIndicator
。IndicatorMode
默认值为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" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论