我有一个窗口,其中包含一个
现在,问题在于有时
下面是一些XAML示例窗口的代码,演示了这个问题...
如果我将第二行网格的高度更改为
我尝试了此博客文章中的
有人知道如何允许
ItemsControl
,里面可以有不定数量的控件。为了考虑到当控件数超过窗口高度时的情况,我将其包装在ScrollViewer
中,这样当项目数超过可用高度时,会显示滚动条。现在,问题在于有时
ItemsControl
中没有任何内容,有时则有。因此,我将网格行的高度设置为Auto
,以允许ItemsControl
在为空时消失或在需要时增长。然而,这意味着该行需要多少高度就占用多少高度,即使这超过了窗口高度,垂直滚动条也永远不会显示。下面是一些XAML示例窗口的代码,演示了这个问题...
<Window x:Class="DuplicateCustomerCheck.TestScrollViewerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test Scroll Viewer Window"
Height="450"
Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Name="N"
TextChanged="TextBoxBase_OnTextChanged"
Grid.Row="0"
Margin="3" />
<Grid Margin="3"
Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Possible duplicate of..."
Margin="3" />
<ScrollViewer VerticalScrollBarVisibility="Visible"
Grid.Row="1">
<ItemsControl Name="MatchingNames"
ItemsSource="{Binding MatchingNames, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Item}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
<TextBlock Grid.Row="2"
Margin="3"
Text="Stuff at the bottom" />
</Grid>
</Window>
为了演示目的,这里是按钮的事件处理程序,允许我测试不同数目的项目(请注意,这是简单粗暴的代码,因此没有错误检查等)...
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e) {
MatchingNames.ItemsSource = Enumerable
.Range(0, int.Parse(N.Text))
.Select(n1 => new {
Item = "Button " + n1
});
}
如果我将第二行网格的高度更改为
*
,那么它就能正常工作,但这意味着ItemsControl
将永久可见,而我不想要这样。它应该仅在其中有一些项时才显示。我尝试了此博客文章中的
ScrollViewerMaxSizeBehavior
行为(代码在这里),但没有任何效果。有人知道如何允许
ItemsControl
使用它所需的所有垂直空间(包括零),但不要增长到超出窗口大小吗?