Search code examples
c#wpfxamlresizegrid-layout

How to prohibit certain cells from resizing row or column


I am creating a 2x2 grid layout in XAML. The top left is a groupbox. The top right is a listbox, with a visible scrollbar. The bottom is a double-width groupbox. As items are added to the top right listbox, I do not want the row to resize. Instead, I want the scroll bar to become enabled. However, as the top left groupbox has items added, I DO want the row to resize to fit the content.

I have tried playing with the row definitions and gridsplitter, but nothing seems to work how I want it.

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="auto"/>
      <ColumnDefinition Width="*"/>
   </Grid.ColumnDefinitions>
   <Grid.RowDefinitions>
      <RowDefinition Height="auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <GroupBox x:Name="TopLeftGroupBox"/>
    <ListBox x:Name="TopRightListBox" Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Visible"/>
    <GroupBox x:Name="BottomGroupBox" Grid.Row="1" Grid.ColumnSpan="2"/>
</Grid>

Currently, as items are added to the top right listbox, Row 0's height increases and the top right scrollbar remains disabled. I want only the top left groupbox to be able to resize the row height. As the top left listbox requires more space, it should enable the scrollbar instead of resizing.

Please let me know if you have any ideas on how to lock a cell from resizing its row.

Thank you.


Solution

  • Try this.

    <ListBox x:Name="TopRightListBox" Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Visible"
                     Height="{Binding ElementName=TopLeftGroupBox, Path=ActualHeight}"/>