2010-09-12 31 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