views:

223

answers:

2

Hi,

I'm using a Grid in WPF (xaml) and I'm have some strange effect when using the MinWidth property in a ColumnDefinition. For example, when I use 9 ColumnDefinition and every ColumnDefinition has the 'Width="*"' property and one of the middle columns also has a MinWidth property then the size of the other columns is wrong.

Well, it's hard to discribe but this xaml code illustrates it nicely:

  <Grid Width="500">  
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*" MinWidth="250"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" Background="Green"/>
    <Border Grid.Column="1" Background="Blue"/>
    <Border Grid.Column="2" Background="Red"/>
    <Border Grid.Column="3" Background="Yellow"/>
    <Border Grid.Column="4" Background="Purple"/>
    <Border Grid.Column="5" Background="Orange"/>
    <Border Grid.Column="6" Background="Azure"/>
    <Border Grid.Column="7" Background="LightBlue"/>
    <Border Grid.Column="9" Background="LightGreen"/>
  </Grid>

When you run this xaml code you'll see that the first 3 columns have a different width than the last 5 columns. Where I expected all of those to have the same width.

Does anyone know if this is a bug. And if there is a way to do this correctly.

Thanks in advance.

A: 

It looks like it just the way it works. You've limited grid in 500 points, and said: hey, give all grid columns the same width, but also this column should be at least 250 points. Now the question from WPF to you: Dude, I see you asked me to give each of 9 column at least 250 points, how can I do this in 500 points? And it makes a decision, to respect your minimum width, but the price is - width of the rest columns.

As for the way to do this correctly. What do you mean? What do you want?

Anvaka
I would want to give all the columns the same width. But when the middle column gets too small it should have a minimum width but the remaining space should be evenly distributed over the other columns. For example, when you replace MinWidth="250" with MaxWidth="10" in the above example you'll see that the remaining space gets evenly distributed. So it works for MaxWidth but not for MinWidth?
J W
Oh. I see your point. And after Rob showed that workaround, I tend to agree. It's a bug. Sorry for not being helpful but glad you could find a solution :)
Anvaka
+3  A: 

I see what you mean - the columns to the left of the yellow one are wider than the columns to the right, even though they are meant to be given the same proportions.

I would say it's a bug, especially when you consider that the following workaround works:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="1.0000001*" MinWidth="250"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

I would guess that this bug is related to how the columns are grouped by width...

Rob Fonseca-Ensor
Perfect answer and a very elegant solution. Thanks.
J W
Very interesting, Rob! Good catch.
Anvaka