开发者

How can I check for child control clipping in a WPF ScrollViewer?

I'm trying to use the WPF ScrollViewer with a stackpanel containing buttons, and without the scrollbar.

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

I want to use a "scroll up" and "scroll down" button elsewhere on the window (this is likely to be used on a开发者_JS百科 small in vehicle screen). Easy enough to do using the scrollViewer1.LineDown() etc, but I would like to only show the "scroll up/scroll down" button if there are elements clipped or outside the viewport.

I'm not sure how to start here - do I need to test each element?

Any pointers most welcome!

Regards, Jason


This code will enable/disable the up and down buttons.

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>

Code behind:

private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
   _upButton.IsEnabled = (sender as ScrollViewer).VerticalOffset > 0;
   _downButton.IsEnabled = (sender as ScrollViewer).VerticalOffset < (sender as ScrollViewer).ScrollableHeight;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜