我将一个 ListView 放在一个 View 的中间行。该 View 包含在一个将 SizeToContent 设置为 WidthAndHeight 的窗口中。 ListView 最初是空的,但是底层的 ViewModel 在此过程中填充了这个 ListView 。
中间的 Grid.Row 高度设置为 * 以填充窗口的可用大小。当 ListView 接收到新项目时,它会在某个时候扩大窗口大小,而不是在 ListView 中显示 ScrollViewer。如何防止这种行为,将 SizeToContent 设置为 WidthAndHeight,将 Grid.Row 高度设置为 * 但不让 ListView 扩展窗口尺寸?
这是窗口的代码(Workspace 属性将包含 ViewModel):
<Window x:Class="Views.ContainerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Title}"
SizeToContent="WidthAndHeight">
<ContentControl Content="{Binding Workspace}"/>
</Window>
提供的 ViewModel 的 View 如下所示:
<UserControl x:Class="Views.SomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="450">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
TextWrapping="Wrap"
Margin="5"
Text="Some description text"/>
<ListView Grid.Row="1"
ItemsSource="{Binding ItemsList}"
Margin="5">
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
</ListView>
<Button Grid.Row="2"
HorizontalAlignment="Right"
Command" Value="{Binding CloseCommand}"/>
</Grid>
</UserControl>
最佳答案
加载完成后关闭窗口自动调整大小对您有用吗?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.SizeToContent = SizeToContent.Manual;
}
关于c# - 如何防止 ListView 扩展窗口维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694883/