2014-10-01 84 views
0

看起來很簡單,但難以置信。ListView佈局

我想有這樣的佈局:

這是ListView應採取按鈕的最大高度(可見按鈕的數量是動態的)。

我想這

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    ... 

    <Grid Grid.Row="1"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 

     <StackPanel> 
      <Button/> 
      <Button/> 
      ... 
     </StackPanel> 

     <ListView Grid.Column="1"/> 

    </Grid> 
</Grid> 

問題:ListView有一個非常奇怪的行爲,大小自我的內容。如果我添加很多項目,那麼突然需要所有的窗口空間。

問題:如何限制ListView高度不超過按鈕總高度(StackPanel高度)?

P.S .: mvvm,所以純xaml更可取。

P.S.S .:我覺得它會對某物產生約束。但是什麼?

+0

將按鈕和列表視圖放在它自己的面板中,然後只是擔心左右對齊? – Sayse 2014-10-01 12:43:10

+0

@Sayse,爲什麼這會有所幫助?什麼會阻止'ListView'使面板更大和成長? – Sinatr 2014-10-01 12:47:16

+0

,因爲你的高度限制在它的主人的大小,然後你確定面板的高度是固定的 – Sayse 2014-10-01 12:48:23

回答

1

你只需要數據綁定的ActualHeightStackPanelListView.Height使得ListView不會增長大於該ButtonStackPanel

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Grid Grid.Row="1"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <StackPanel Name="ButtonPanel" Grid.Column="0"> 
      <Button Content="Click" /> 
      <Button Content="Click" /> 
      <Button Content="Click" /> 
     </StackPanel> 
     <ListView Grid.Column="1" ItemsSource="{Binding Tests}" 
      Height="{Binding ActualHeight, ElementName=ButtonPanel}" /> 
    </Grid> 
</Grid> 
+0

你又救了我!謝謝。 (是的,我不能單獨解決這個問題,我感到羞愧) – Sinatr 2014-10-01 12:55:14

+0

我們生活和學習。這在過去的某個時候也會讓我難堪。 :) – Sheridan 2014-10-01 13:05:07

1

你都可以在較低的屬性MaxHeight綁定內容網格到ActualHeight的按鈕StackPanel。此外,您需要將按鈕面板的VerticalAlignment設置爲Bottom

這樣,您可以垂直添加更多零件,但仍然堅持按鈕面板的高度。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    ... 

    <Grid Grid.Row="1" 
      MaxHeight="{Binding ElementName=ButtonPanel, Path=ActualHeight}"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 

     <StackPanel Name="ButtonPanel" 
        VerticalAlignment="Bottom"> 
      <Button Content="Button"/> 
      <Button Content="Button"/> 
      <Button Content="Button"/> 
     </StackPanel> 

     <ListView Grid.Column="1"/> 

     <!-- Multiple controls can be added here, 
      but the lower part will still stick 
      to the size of the buttons panel --> 
    </Grid> 
</Grid> 
+0

這也工作,謝謝。但它比Sheridan的解決方案更可* *(不太明顯)。 – Sinatr 2014-10-01 13:02:43