如何防止ListView扩大窗口尺寸?

发布于 2024-09-19 17:41:53 字数 1805 浏览 7 评论 0原文

我将 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>

所提供的 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>

I put a ListView in the middle row of a View. The view is contained in a window that has SizeToContent set to WidthAndHeight. The ListView is initially empty, but the underlying ViewModel fills this list view in the process.

The middle Grid.Row height is set to * to fill the available size of the window. When the ListView receives new items, it will at some point expand the window size instead of displaying the ScrollViewer in the ListView. How can I prevent this behavior to have SizeToContent set to WidthAndHeight, the Grid.Row height to * but not have the ListView expand the window dimensions?

Here's the code for the window (the Workspace property will contain the 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>

The View for the provided ViewModel looks like this:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把回忆走一遍 2024-09-26 17:41:53

加载后关闭窗口自动调整大小对您有用吗?

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.SizeToContent = SizeToContent.Manual;
    }

Would turning off the window autosizing after it has loaded work for you?

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.SizeToContent = SizeToContent.Manual;
    }
看透却不说透 2024-09-26 17:41:53

您必须限制层次结构中动态调整大小的元素之一。即,将 windown、网格或列表框的 maximumheight/maximumwithheight/with 属性设置为一个合适的值。

You will have to restrict one of the dynamically sized elements in the hierarchy. I.e. set maximumheight/maximumwith or the height/with properties of the windown, the grid, or the listboxt to an appropriate value.

笙痞 2024-09-26 17:41:53

正如 AxelEckenberger 十多年前所写的那样,要实现此目的,您必须将 ListView 的 MaxHeight 设置为等于其父级的高度。不幸的是,他没有解释如何实现这一目标。

以下是如何使任何控件适合其父级:

<ParentControl>
    <ChildControl Height="200" 
        MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
            AncestorType={x:Type ParentControl}}, Path=ActualHeight,
            Mode=OneWay}">
    </ChildControl>
</ParentControl>

注意:

  • 绑定到 ActualHeight 而不是 Height 可防止在 Visual Studio 的“XAML 绑定失败”视图中出现警告表示 NaN 不是 MaxHeight 的有效值。
  • 严格来说,Mode=OneWay 是不必要的,因为 MaxHeight 不太可能改变,但它可以记录这样一个事实:我们只期望 MaxHeightActualHeight 设置,我们绝不希望发生相反的情况。

As AxelEckenberger wrote more than 10 years ago, to achieve this you will have to set the MaxHeight of the ListView to be equal to the height of its parent. Unfortunately, he did not explain how to achieve this.

Here is how to make any control fit within its parent:

<ParentControl>
    <ChildControl Height="200" 
        MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
            AncestorType={x:Type ParentControl}}, Path=ActualHeight,
            Mode=OneWay}">
    </ChildControl>
</ParentControl>

Notes:

  • Binding to ActualHeight instead of Height prevents a warning from appearing in the "XAML Binding Failures" view of Visual Studio saying that NaN is not a valid value for MaxHeight.
  • Strictly speaking, Mode=OneWay is unnecessary, since MaxHeight is unlikely to change, but it serves to document the fact that we only expect MaxHeight to be set from ActualHeight and we never intend the opposite to happen.
笑叹一世浮沉 2024-09-26 17:41:53

您实际上可以将 ListView 放入 Dockpanel 中,对我来说效果很好:

<Grid x:Name="QueryNinjasGrid" Grid.Row="1">
    <DockPanel>
        <Button DockPanel.Dock="Top" x:Name="QueryNinjasButton" Content="Query Ninjas" FontSize="20" Margin="20" MaxWidth="150" Click="QueryNinjasButton_Click"/>
        <ListView x:Name="NinjaListView" VerticalAlignment="Stretch" MinHeight="150" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="False" >
            <ListView.View>
                <GridView>
                     <GridViewColumn Header="Id" Width="20" DisplayMemberBinding="{Binding Id}"/>
                     <GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding Name}" />
                 </GridView>
             </ListView.View>
        </ListView>
    </DockPanel>
</Grid>

You can actually put your ListView into a Dockpanel, works fine for me:

<Grid x:Name="QueryNinjasGrid" Grid.Row="1">
    <DockPanel>
        <Button DockPanel.Dock="Top" x:Name="QueryNinjasButton" Content="Query Ninjas" FontSize="20" Margin="20" MaxWidth="150" Click="QueryNinjasButton_Click"/>
        <ListView x:Name="NinjaListView" VerticalAlignment="Stretch" MinHeight="150" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="False" >
            <ListView.View>
                <GridView>
                     <GridViewColumn Header="Id" Width="20" DisplayMemberBinding="{Binding Id}"/>
                     <GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding Name}" />
                 </GridView>
             </ListView.View>
        </ListView>
    </DockPanel>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文