2013-06-26 32 views
1

即使在向列表框中添加多個項目後,如何獲得我的列表框作爲行的標準高度?我不想在上面設置一個高度。我已經嘗試設置一些共享的大小組,嘗試將每個行的高度綁定到不同的行,等等,但沒有解決方案。我希望列表框的四個行的高度與原來的高度相同。該列表框目前增長以適應其中的所有項目,因此使行高,這是我不想要的。如果列表框高於4個「常規」行高,我希望列表框能夠獲得滾動條。如果我必須在列表框上設置一個maxHeight,我怎麼能告訴它是我其他行的高度的4倍?如何讓WPF列表框始終是4個網格行的高度?

XAML:

enter image description here

做些什麼:

<Window x:Class="SharedSizes.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Border Margin="10" > 
    <StackPanel Grid.IsSharedSizeScope="True"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="A"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="B"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="C"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="D"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="E"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="F"/> 

      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Name="Row1" Height="Auto" SharedSizeGroup="Width"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 

      <!--Left Side Parts--> 

      <Label Content="Vendor:" Grid.Row="0" Grid.Column="0"/> 
      <ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" /> 

      <Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" /> 
      <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Width="125"/> 

      <Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/> 
      <ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" /> 

      <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Left"> 
       <Button Content="1"/> 
       <Button Content="2"/> 
       <Button Content="3"/> 
       <Button Content="4"/> 
      </StackPanel> 
      <Label Content="Clients" Grid.Row="2" Grid.Column="5"/> 
     </Grid> 
     <Grid> 
      <Grid.RowDefinitions> 
       <!--<RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}"/> 
       <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /> 
       <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /> 
       <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />--> 
       <RowDefinition SharedSizeGroup="Width"/> 
       <RowDefinition SharedSizeGroup="Width"/> 
       <RowDefinition SharedSizeGroup="Width"/> 
       <RowDefinition SharedSizeGroup="Width"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="A"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="B"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="C"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="D"/> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 

      <Label Content="Fred" Grid.Row="0"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="1"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="2"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="3"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="4"/> 
      <Label Content="Fred" Grid.Row="1"/> 
      <Label Content="Fred" Grid.Row="2"/> 
      <Label Content="Fred" Grid.Row="3"/> 
      <ListBox Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4" Grid.IsSharedSizeScope="True"/> 
     </Grid> 
    </StackPanel> 

如何,我希望它永遠看(以及它如何在列表框中沒有任何項目一樣)在我將項目添加到列表框後:

enter image description here

編輯的VIV的建議:

<Window x:Class="SharedSizes.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <!--<Style TargetType="{x:Type RowDefinition}"> 
     <Setter Property="Height" Value="25"/> 
    </Style>--> 
</Window.Resources> 

<Border Margin="10" > 
    <StackPanel> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 

      <!--Left Side Parts--> 

      <Label Content="Vendor:" Grid.Row="0" Grid.Column="0" /> 
      <ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" /> 

      <Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" /> 
      <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" /> 

      <Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/> 
      <ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" /> 

      <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" > 
       <Button Content="1"/> 
       <Button Content="2"/> 
       <Button Content="3"/> 
       <Button Content="4"/> 
      </StackPanel> 
     </Grid> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}"/> 
       <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
       <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
       <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 

      <Label Content="Fred" Grid.Row="0" x:Name="SomeLabel"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="1"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="2" /> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="3"/> 
      <Label Content="Fred" Grid.Row="0" Grid.Column="4"/> 
      <Label Content="Fred" Grid.Row="1"/> 
      <Label Content="Fred" Grid.Row="2"/> 
      <Label Content="Fred" Grid.Row="3"/> 
      <ListView Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4" /> 
     </Grid> 
    </StackPanel> 
</Border> 

Using Viv's suggestions

+0

如果它跟蹤回汽車的尺寸它會成長爲適應內容。 – Paparazzi

+0

我想你或者需要設置列表框的高度,或者設置網格行的高度。你有什麼理由反對嗎?事實上,你最外層的容器是一個堆棧面板,可能有問題,並且它們通常具有非常不合理的尺寸。 –

+0

如果我必須把它設置到一個高度,我怎麼能告訴它4倍的其他行?我希望我所有的行基本上都是相同的高度。我想我必須爲行選擇一個高度並將其應用於所有行? –

回答

1

那麼你可以通過設置做到這一點Grid.RowHeight使用其他項目的DesiredSize.Height(該Label「每一行都有)。

所以通過切換:

... 
<!--<RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}"/> 
    <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /> 
    <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /> 
    <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />--> 
... 
<Label Grid.Row="0" 
     Content="Fred" /> 
... 

<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" /> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" /> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" /> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" /> 
... 
<Label x:Name="SomeLabel" 
     Grid.Row="0" 
     Content="Fred" /> 
... 

我只是結合所有4 RowDefinition.Height到同一Label的,因爲所有行的具有相同的項目內容和風格反正,但你有希望得到這張照片。如果它們在每一行中都是不同的項目,給它們一個對應的名稱,並將每個RowDefinition綁定到相應的控件。

這將產生:

enter image description here

更新:

你有一個錯字,當你已經盡我的建議,

... 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" /> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
<RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/> 
... 

只排一個是正確的。

這是DesiredSize沒有DeisredSize

+0

@ Viv-我在嘗試了你的建議後編輯了原始帖子以顯示結果。我仍然看到列表框越來越大,因此行也變得高度過大。與您的解決方案有什麼不同? –

+1

@ user1029770在帖子底部檢查我的更新。 – Viv

+0

太棒了!感謝您的幫助,這正是我現在想要的。這是小事,對吧?! –