【问题标题】:How can I check for child control clipping in a WPF ScrollViewer?如何检查 WPF ScrollViewer 中的子控件剪辑?
【发布时间】:2011-02-28 21:37:45
【问题描述】:

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

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

欢迎任何指点!

问候,杰森

【问题讨论】:

    标签: wpf scrollviewer


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢您的快速回复 - 完全符合要求!
    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2012-01-31
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多