我怎样才能让我的列表框成为我的行的标准高度,即使在我向列表框添加了几个项目之后?我不想在任何一个上设置高度。我尝试设置一些共享大小组,尝试将每行的高度绑定到不同的行等,但看不到解决方案。我希望列表框占据的四行的高度与它们原来的高度相同。列表框当前增长到适合其中的所有项目,因此使行更高,这是我不想要的。如果列表框高于 4 个“正常”行高,我希望它只获得滚动条。如果我必须在列表框上设置一个 maxHeight,我怎么能告诉它是我其他行之一的高度的 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>