如何在WPF ScrollViewer中检查子控件剪辑?

时间:2010-06-07 20:53:38

标签: wpf scrollviewer

我正在尝试将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()等很容易,但我想只显示“向上/向下滚动”按钮,如果有截断的元素或视口外。

我不知道如何从这里开始 - 我是否需要测试每个元素?

欢迎任何指示!

问候,杰森

1 个答案:

答案 0 :(得分: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;
}