【发布时间】:2014-04-05 03:34:27
【问题描述】:
我的 WPF 应用程序有一个包含ListBox 的窗口。出于某种我不明白的原因,在将项目插入其中后,它今天开始在屏幕上生长。我希望它的高度保持固定,并且我不希望它在每次插入项目时都增加。
这是窗口的 XAML:
<Viewbox Stretch="Uniform">
<Grid Background="{DynamicResource WindowBackground}"
Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Background="{DynamicResource AlarmTitleBackground}"
Grid.Row="0"
MouseLeftButtonDown="LeftMouseButtonDown">
. . .
</Grid>
<Rectangle Fill="{DynamicResource AlarmTitleBackground}"
Grid.Row="1"
Height="4" />
<Grid Background="{DynamicResource ControlBackground}"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid FocusManager.IsFocusScope="True"
Grid.Column="0"
Grid.Row="0"
Name="PendingAlarmScope">
<ListBox Background="{DynamicResource TextBackground}"
HorizontalContentAlignment="Center"
IsSynchronizedWithCurrentItem="True"
Margin="5"
MinWidth="185"
VerticalAlignment="Stretch"
Name="AlarmsList"
SelectionChanged="AlarmsList_SelectionChanged" />
</Grid>
. . .
</Grid>
</Grid>
</Viewbox>
我在博客中发现了一篇关于 TextBox 的帖子,该帖子随着字符的输入而不断增长。作者指出TextBox 在ScrollViewer 中,HorizontalScrollBarVisibility 属性设置为“自动”。他们将其更改为“禁用”,这为他们修复了它。我尝试添加另一个控件的 ScrollViewer.VerticalScrollBarVisiblility="Disabled" to the xaml but this didn't work. I also tried binding theListBox 的 MaxHeight property to theActualHeight`,该控件的高度与我想要的完全相同,但也没有用。
如何在不设置的情况下修复ListBox 的Height?我希望ListBox 始终填充它所在的Grid 单元格,并且窗口可以根据不同的屏幕分辨率自行增长和重新缩放。
【问题讨论】:
-
您可以尝试使用此处提供的破解方法吗? stackoverflow.com/questions/386039/…
-
这不起作用,但我相信这是因为
ViewBox。请参阅我对 Blam 回答的评论。 -
@MikeNakis:非常感谢,它可能会解决问题。不幸的是,我自 2016 年以来一直没有这份工作,这个问题与我不再相关。不过,如果我再次遇到这种情况,那就太好了。