我在 silverlight 应用程序的边框内的堆栈面板内有一个列表框,当我向列表框中添加任何内容时,它的高度就会增加,因此滚动条永远不会被使用,并且它会延伸到边框元素的边界之外。我已经尝试显式设置列表框、边框和堆栈面板的高度属性,但它仍然超出了这个范围。
这是我的代码:
<Border x:Name="articlePane">
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button Content="Latest" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Pending" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Done" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
</StackPanel>
<ListBox x:Name="articleList" Margin="5" Background="Transparent" ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Center">
<Image x:Name="articleImage" />
<TextBlock x:Name="articleTitle" Text="{Binding Path=Title}" FontSize="18" FontWeight="Bold"
Margin="5"/>
<TextBlock x:Name="articleDate" Text="{Binding Path=Date}" FontSize="14" Foreground="Gray"
Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
最佳答案
我有一种感觉,StackPanel 会永远增加高度。我认为在这种情况下,您最好在边框内使用网格布局。将 Button StackPanel 放在一个高度定义的行中,将 ListBox 放在一个自动调整大小 * 高度的行中。这样网格就会固定在表单的范围内。
关于c# - 列表框延伸到页面之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4316471/