c# - WPF如何使StackPanel可滚动

标签 c# .net wpf visual-studio-2010 stackpanel

在堆栈面板中,我在运行时从代码后面添加了一些标签:我想使堆栈面板可滚动。在xaml文件中,我有:

<ScrollViewer HorizontalAlignment="Left" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto">

    <StackPanel x:Name="stackPanelVistaProfiloTessera" Height="292" Width="170"/>

</ScrollViewer>

在后面的代码中,我向堆栈面板添加了一些标签:
for(.....)
{
    stackPanelVistaProfiloTessera.Children.Add(new Label {....});
}

为什么堆栈面板无法滚动?我该如何解决?

谢谢

最佳答案

从stackpanel移除高度和宽度。.rest在这里工作正常。

<ScrollViewer HorizontalAlignment="Left" Background="Green" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto">
    <StackPanel x:Name="stackPanelVistaProfiloTessera"  Background="RoyalBlue" >
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
        <Label Height="30" Width="100" Margin="5">label1</Label>
    </StackPanel>
</ScrollViewer>

输出看起来像这样。

如果您将高度和宽度用于设计目的,则可以将边距设置为stackpanel。

关于c# - WPF如何使StackPanel可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168565/

相关文章:

c# - 防止 COM 名称重整

"is"关键字的 C# 使用模式

c# - 使用 nHibernate 实体和 DTO 发送网格数据

c# - 我可以内联指定我的显式类型比较器吗?

c# - 如何将文件路径数组分成几个较小的数组?

c# - 在集合中存储实现通用接口(interface)的对象

c# - 如何在代码中设置 ListBox 控件的 Style 属性

c# - 记录接口(interface)及其实现

c# - OpacityMask 无法按预期在 C# 代码中工作

c# - 跨序列化对象反射以设置 PropertyChanged 事件