Grid Column Width - mix *(star), Auto and fill lef

2019-06-11 05:02发布

问题:

I have a Grid width following Column Definitions:

<Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="320" MaxWidth="450" Width=".32*"/>
        <ColumnDefinition MinWidth="200" Width="Auto"/>
        <ColumnDefinition MinWidth="200" Width="Auto"/>
        <ColumnDefinition Width="Auto"/> <- (4) should fill all available space
        <ColumnDefinition MinWidth="250" MaxWidth="300" Width=".20*"/>
</Grid.ColumnDefinitions>

The problem is that setting width to Auto on column (4) zeros it's width and last column is next to third one. I'd like to have last column aligned to right edge of app and first three to the left.

I also can't set (4) width to star(*) - obviously it stretches relatively to first and last column.

I've been trying for a while with horizontalalignments but as a result - content is not filling the columns width. I could maybe bind content width to column width or sth like that, but I'm curious whether there is some easy way to achive the (4) column fill all left space.

回答1:

I think you didnt quite undestand the (*) which stands for weighted average.

so for your case.

<Grid.ColumnDefinitions>
     <ColumnDefinition MinWidth="320" MaxWidth="450" Width="3*"/>
     <ColumnDefinition MinWidth="200" Width="Auto"/>
     <ColumnDefinition MinWidth="200" Width="Auto"/>
     <ColumnDefinition Width="5*"/>
     <ColumnDefinition MinWidth="250" MaxWidth="300" Width="2*"/>
 </Grid.ColumnDefinitions> 

if you wish to make the column (4) small reduce the * weightage and increase where you like.

Hope it helps...