如何防止ListView扩展窗口维度?

Hol*_*dam 5 c# wpf height listview viewmodel

我将ListView放在View的中间行.该视图包含在SizeToContent设置为WidthAndHeight的窗口中.ListView最初为空,但底层的ViewModel在此过程中填充此列表视图.

中间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>
Run Code Online (Sandbox Code Playgroud)

提供的ViewModel的视图如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

小智 5

在为你加载工作后,关闭窗口会自动调整吗?

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.SizeToContent = SizeToContent.Manual;
    }
Run Code Online (Sandbox Code Playgroud)