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

标签 c# wpf listview height viewmodel

我将一个 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/

相关文章:

jquery - 将 Galleria jQuery 插件与 asp.net ListView 结合使用

c# - 文本过多的 GridView

c# - 在 app.config 文件中写一段

c# - .Net 中的文件操作是否有异步替代方案?

wpf - Entity Framework CTP5 代码优先,WPF - MVVM 建模

android - 更改由 CustomAdapter(扩展 BaseAdapter)创建的微调器上的文本

c# - 使用 DrawString 将单个字符居中

路由中的 C# WebAPI 核心反斜杠

c# - 命令参数始终为 NULL

android - 在什么情况下适配器不会更新附加 View