即使在向列表框中添加多個項目後,如何獲得我的列表框作爲行的標準高度?我不想在上面設置一個高度。我已經嘗試設置一些共享的大小組,嘗試將每個行的高度綁定到不同的行,等等,但沒有解決方案。我希望列表框的四個行的高度與原來的高度相同。該列表框目前增長以適應其中的所有項目,因此使行高,這是我不想要的。如果列表框高於4個「常規」行高,我希望列表框能夠獲得滾動條。如果我必須在列表框上設置一個maxHeight,我怎麼能告訴它是我其他行的高度的4倍?如何讓WPF列表框始終是4個網格行的高度?
XAML:
做些什麼:
<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>
如何,我希望它永遠看(以及它如何在列表框中沒有任何項目一樣)在我將項目添加到列表框後:
編輯的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>
如果它跟蹤回汽車的尺寸它會成長爲適應內容。 – Paparazzi
我想你或者需要設置列表框的高度,或者設置網格行的高度。你有什麼理由反對嗎?事實上,你最外層的容器是一個堆棧面板,可能有問題,並且它們通常具有非常不合理的尺寸。 –
如果我必須把它設置到一個高度,我怎麼能告訴它4倍的其他行?我希望我所有的行基本上都是相同的高度。我想我必須爲行選擇一個高度並將其應用於所有行? –