1

我正在尝试将 WPF ScrollViewer 与包含按钮的堆栈面板一起使用,并且没有滚动条。

    <ScrollViewer Grid.Row="1" Name="scrollViewer1" VerticalScrollBarVisibility="Hidden">
        <StackPanel Name="stackPanel1">
            <Button Content="Button1" Height="23" Name="button1" MinHeight="75" />
            <Button Content="Button2" Height="23" Name="button2" MinHeight="75" />
            <Button Content="Button3" Height="23" Name="button3" MinHeight="75" />
            <Button Content="Button4" Height="23" Name="button4" MinHeight="75" />
            <Button Content="Button5" Height="23" Name="button5" MinHeight="75" />
        </StackPanel>
    </ScrollViewer>

我想在窗口的其他地方使用“向上滚动”和“向下滚动”按钮(这很可能用于小型车载屏幕)。使用 scrollViewer1.LineDown() 等很容易,但如果有元素被裁剪或在视口之外,我只想显示“向上/向下滚动”按钮。

我不确定如何从这里开始 - 我需要测试每个元素吗?

欢迎任何指点!

问候,杰森

4

1 回答 1

0

此代码将启用/禁用向上和向下按钮。

XAML:

<Window x:Class="ScrollTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0" ScrollChanged="OnScrollChanged">
            <StackPanel>
                <Button Content="Button1" Height="50" />
                <Button Content="Button2" Height="50" />
                <Button Content="Button3" Height="50" />
                <Button Content="Button4" Height="50" />
                <Button Content="Button5" Height="50" />
            </StackPanel>
        </ScrollViewer>
        <Button Grid.Row="1" Name="_upButton" Content="Up" />
        <Button Grid.Row="2" Name="_downButton" Content="Down" />
    </Grid>
</Window>

后面的代码:

private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
   _upButton.IsEnabled = (sender as ScrollViewer).VerticalOffset > 0;
   _downButton.IsEnabled = (sender as ScrollViewer).VerticalOffset < (sender as ScrollViewer).ScrollableHeight;
}
于 2010-06-07T21:20:29.833 回答