【问题标题】:Scrollbars in a dynamically filled stack panel动态填充堆栈面板中的滚动条
【发布时间】:2014-04-05 08:53:22
【问题描述】:

我是 WPF 的新手,如果答案如此明显,我深表歉意。 我得到了一个带有单个堆栈面板控件的 WPF 可调整大小的窗口,该控件垂直和水平拉伸以填充窗口。 在窗口激活事件中,我使用“.Children.Add”将按钮控件添加到面板。我不知道运行时会有多少按钮,所以我在面板中检查了“CanVerticallyScroll”。 ScrollViewer.VerticalScrollBarVisibility 默认设置为 Visible。

我在运行时仍然看不到滚动条。

我错过了显示带有按钮的滚动面板的哪些属性?

谢谢

XAML:

<Window x:Class="ResMed.Ecp.Utility.ConnectionWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ConnectionWindow" Height="388" Width="641.6" Activated="Window_Activated">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="359*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="pnlConnectionButtons" Margin="10,10.2,10.2,10" Grid.Row="1" CanVerticallyScroll="True"/>

    </Grid>
</Window>

后面的代码:

private void Window_Activated(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        Button btn = new Button();
        btn.Content = "Hello";
        pnlConnectionButtons.Children.Add(btn);
    }
}

【问题讨论】:

  • 能否请您在您遇到问题的地方发布 xaml 和代码?
  • Evan,已将代码添加到问题中。

标签: c# wpf button scrollbar stackpanel


【解决方案1】:

将您的StackPanel 放入ScrollViewer

<ScrollViewer>
    <StackPanel>
        <Button Content="Hello World"></Button>
        ...
        ...
    </StackPanel>
</ScrollViewer>

您也可以删除CanVerticallyScroll="True"。来自MSDN

此属性不适用于您的代码。它公开公开以履行接口契约 (IScrollInfo)。设置此属性无效。

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多