Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WPF – GridRows Spacing Confusion

I have an Expander control, and the grid inside will have a ListBox with a Label on top of it saying ‘Video Sources’. I am attempting to use Grid Row Definitions to achieve this. My issue however is that the grid rows separate everything evenly. I want the label to be directly on top of the ListBox. Removing the definitons causes the ListBox to fill up the entire grid including covering up the Label (which makes no sense to me as the label is on top).

My current code is below:

<Expander HorizontalAlignment="Left" Height="434" Header="Expander" ExpandDirection="Left" Margin="651,8,0,8">
    <Grid Background="#FF252525" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Label Content="Video Sources" Grid.Row="0"/>

        <ListBox Grid.Row="1" d:ItemsSource="{d:SampleData}">

        </ListBox>
    </Grid>
</Expander>

The code produces this result. You can see there are even gaps between each control. I want the video sources label right above the listbox:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

It would be nice if you could set the column name like in a ListView, however as far as I am aware that is not possible. I don’t think it’s worth using a ListView for something that will only have a single column, either

>Solution :

You have to set the rows height ; to auto (ie: minimal value) and * (ie: remaining space).
Also only two rows definition are needed.

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

    <Label Grid.Row="0"
           Content="Video Sources" />

    <ListBox Grid.Row="1"
             ItemsSource="{d:SampleData}"
             VerticalAlignement="Top" />
</Grid>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading