tags:

views:

53

answers:

1

Here is a 2x2 grid, in which the first column spans both rows, but its fixed-height content seems to be dictating the minimum height for the first row:

<UserControl x:Class="Test"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="300">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Rectangle Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Fill="Green"
      Width="50" Height="50" VerticalAlignment="Top" />

    <TextBlock Grid.Row="0" Grid.Column="1" Margin="3" TextWrapping="Wrap">
        Some text goes here. 
    </TextBlock>

    <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right">OK</Button>
  </Grid>
</UserControl>

In this example, the first row is always at least 50 high, as is evident from the space consumed by the TextBlock, even though I'd like it (and as a consequence, the entire control) to be shorter if there isn't much text to display. Am I missing something, or is this a WPF layout bug?

(Yes, I know I could easily code it with nested grids, but this is a simplified distillation of a more complicated case.)

A: 

What you should be seeing is the Button taking as much height as it needs and the TextBlock getting the rest of the control's available height. Since Column 0 only has the fixed size Rectangle in it, it will always have a width of 50, with Column 1 taking the rest of the available width. Are you seeing something other than that or is there something different that you want the layout to do?

John Bowen
You are correct about what column 1 should show. The problem is in column 0, where the fixed height of the object is forcing a minimum height on the contents of row 0, column 1, when the column 0 should be instead consuming all of the height of the control. What I'm seeing is that row 1 contains only the button, not the bottom portion of the 0,0, object.
vanmelle
If the Grid's total height is less than Button.ActualHeight+50 the Rectangle will show as stretching across both rows but it won't otherwise since it has a fixed height and is aligned to Top.
John Bowen
The first part is true, but that's not the question. I don't want to set the height of the UserControl, I want it to be just as tall as it needs to be and no more. And the VerticalAlignment=Top is not the answer, either. I can remove that, and the Rectangle will center itself in the first column, but still force the first row's height to be at least 50. That seems to me to be a bug. Maybe I should go report it to MS and see what they say.
vanmelle
What you're describing is the behavior of 2 Auto Rows, not a * and an Auto.
John Bowen
Although I don't see why (*, Auto) should not do what I want, you're correct that (Auto, Auto) gets my desired outcome, and there's no particular reason for me not to have used it. Thanks.
vanmelle
Essentially (there's more to how the space is split up), * means size to parent, while Auto means size to content. What you wanted was the UC to ignore the available space of the parent and size based on its content.
John Bowen