2010-09-12 29 views
4

我把一个ListView放在视图的中间一行。该视图包含在SizeToContent设置为WidthAndHeight的窗口中。 ListView最初是空的,但底层的ViewModel在这个过程中填充了这个列表视图。如何防止ListView扩大窗口尺寸?

中间Grid.Row高度设置为*以填充窗口的可用大小。当ListView接收到新项目时,它会在某个时候扩大窗口大小,而不是在ListView中显示ScrollViewer。我怎样才能防止这种行为有SizeToContent设置为WidthAndHeight,Grid.Row高度*,但没有ListView展开窗口尺寸?

下面是窗口(工作空间属性将包含视图模型)的代码:

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

为提供视图模型的视图看起来是这样的:

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

回答

4

在为您加载工作之后会关闭窗口自动调整功能吗?

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

谢谢。这实际上是我使用的解决方案。你可以看到,如果你扩展上面的评论。 :) – 2010-10-08 13:34:15

+0

@Ninfendo:+1这是邪恶的,谢谢。我浪费了一个下午试图弄清楚。 – TarkaDaal 2013-02-07 18:13:53

1

你将不得不限制一个动态大小的元素在层次结构中。即设置maximumheight/maximumwithheight/with将windown,网格或listboxt的属性设置为适当的值。

+0

我宁愿不那样做。我正在寻找一种方法来不让ListView展开,如果项目被添加。 – 2010-09-12 13:35:28

+0

我试图将ListView的高度或最大高度绑定到中间Grid.Row的当前高度,但这并没有帮助。 – 2010-09-12 13:36:09

+1

那么你需要限制某些东西。如果列表框可以增长,那么你将看不到滚动面板。但是,利斯波斯的无条件增长导致了窗口的无限制增长。典型的捕捉22.因此,恕我直言,使用窗口或lisbox的最大尺寸会让你在那里你想要的 - 使窗口尽可能小,但如果它太大,滚动条显示在列表框。 – AxelEckenberger 2010-09-12 13:42:36