WPF: Problem with overlapping controls and grid column widths

controlsgridwpf

I have a problem regarding a parent grid with a control in it that overlaps a tabcontrol.

I need the child grid (in the tab control) to resize its columns according to the overlapping control.
Specifically, when the overlapping control is resized (due to resize of the window for example) the child grid inside the tabcontrol needs to resize its columns so that the child controls inside the tabcontrol grid isn't overlapped by the control that overlaps the tabcontrol.

I sincerely hope someone here knows a solution for this problem, I've been fighting with it for days 🙂

Thanks in advance!

Best regards,
Req

edit: In response to the comments below:

Absolutely – I figured I should have, but seeing that I was/am at work I didn't have the code handy. But I can write up a similar example of the XAML.

<Grid Name="parentGrid" >
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="1*" />
    <ColumnDefinition Width="1*" />
    <ColumnDefinition Width="1*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="1*" />
    <RowDefinition Height="1*" />
    <RowDefinition Height="1*" />
  </Grid.RowDefinitions>

  <TabControl Name="tabCtrl" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2">
    <TabItem Name="tabItem1">
      <Grid Name="tabCtrlGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="1*" /> <!-- This is the column I want to resize according to the overlapping image control below -->
          <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="1*" />
          <RowDefinition Height="1*" />
        </Grid.RowDefinitions>

        <Button Name="someChildControl" Grid.Column="1" Grid.Row="0" />
      </Grid>
    </TabItem>
  </TabControl>

  <Image Name="overlappingImg" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2" /> <!-- whenever the screen/window is resized, the parentGrid resizes, and thus resizing this overlapping image. -->
</Grid>

What needs to happen is that column 0 in the tabCtrlGrid needs to resize its width to fit the width of the overlapping area of the image. That way someChildControl is never overlapped by the image, regardless of how it's resized.

Hopefully that makes it a little more clear 🙂

Best Answer

How does this look?

    <Grid Name="parentGrid" Background="LightGray">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Aqua"/>
        <TextBlock Text="Tab controller" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
        <Rectangle Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Aqua"/>
        <TextBlock Text="Up down nav" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        <Grid Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Rectangle HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Height="160" Fill="BlanchedAlmond"/>
            <TextBlock Text="CoverArt" HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Height="160"/>
            <Rectangle Grid.Column="1" Fill="LightGray" />
            <TextBlock Text="Tab content" Grid.Column="1" />
        </Grid>
    </Grid>
Related Topic