Reputation: 2469
How can i get my listbox to be the standard height of my rows, even after i add several items to the listbox? I don't want to set a Height on either. I've tried setting some shared size groups, tried binding the height of each of the rows to a different row, etc with no solution in sight. I want the height of the four rows the listbox takes up to be the same height as they are originally. The listbox currently grows to fit all the items in it, therefore making the rows taller, which i don't want. I want the listbox to just get scrollbars if it is higher than 4 "normal" row heights. If i MUST set a maxHeight on the listbox, how can i tell it to be 4x the height of one of my other rows?
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>
How I want it to look always (and how it does with no items in the listbox):
What it does after i added items to the listbox:
EDITED FOR VIV's SUGGESTIONS:
<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>
Upvotes: 1
Views: 2519
Reputation: 17388
Well you can do this by setting the Grid.Row
's Height
using the DesiredSize.Height
of the other item(the Label
's you have) in each row.
so by switching:
...
<!--<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" />
...
to
<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" />
...
I'm just binding all 4 of the RowDefinition.Height
to the same Label
since all row's have the same item content and style anyways, but you get the picture hopefully. If they are different items in each row, give them a corresponding name and Bind each RowDefinition
to the corresponding control.
This produces:
Update:
You've got a typo when you've tried my suggestion,
...
<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}"/>
...
Only row one is correct.
It's DesiredSize
not DeisredSize
Upvotes: 1