我遇到了 UI 设计问题,这就是我希望我的应用程序窗口的内容缩小以适应窗口的最小尺寸。
使用下面的 XAML,当窗口太窄时,内容会缩小以适应。完美的。我的问题是当窗口太短时,内容会掉到底部,像这样:
<ItemsControl ItemsSource="{Binding GroupStatsDisplayList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Viewbox Margin="4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" >
<ListBox>
<StackPanel Orientation="Horizontal" Margin="5" Background="Blue">
<Label Content="{Binding GroupID}" FontWeight="Bold"/>
<Label Content="{Binding GroupName}" Width="100" FontWeight="Bold" />
<Label Content="{Binding CallsInQueue}" FontWeight="Bold" />
<Label Content="{Binding TSF}" FontWeight="Bold" />
</StackPanel>
</ListBox>
</Viewbox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我已经阅读了 ItemsControl 使用 StackPanel 作为其默认项目面板并且 StackPanel 的行为方式与我所看到的类似问题的答案。建议使用网格来克服像我这样的问题。
所以我试着告诉我的 ItemsControl 使用网格:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
更好,因为在任一方向受到约束时内容都会调整大小。更糟糕的是,似乎只有一个 ViewBox->ListBox 元素会使用我的集合中的最后一项进行更新(当应用程序启动时,我可以通过显示屏看到集合循环中的三个项目)。我没有在屏幕上看到我收藏中的所有项目,只有最后一个。
我还读到 DockPanel 可以救我……
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
那是事情变得疯狂的时候,我决定写这个问题。现在我的集合中的所有项目都在那里,但是当我水平扩展窗口时它们会一一出现,每个新项目都会显示为现有项目扩展到窗口的垂直范围。
如何获得看起来像第一个图像但会缩小以适应窗口最小尺寸的布局?