ColumnDefinition MinWidth doesn't work correctly

狂风中的少年 提交于 2019-12-12 14:34:12

问题


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.


回答1:


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...




回答2:


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?



来源:https://stackoverflow.com/questions/2340233/columndefinition-minwidth-doesnt-work-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!