Uday0119
Uday0119

Reputation: 790

Border control is not filling upto Grid's Row width

I have a grid control where it has been split Column wise.

<Grid HorizontalAlignment="Stretch">
    <Grid.ColumnDefinitions>
         <ColumnDefinition />
         <ColumnDefinition Width="80" />
         <ColumnDefinition Width="65" />
    </Grid.ColumnDefinitions>
</Grid>

I have a border control inside column 0. However I am facing an issue that border control is not filling upto the width of this column. The border contains a textblock with Wrapping enabled. if the text in textblock is bigger than width, then it gets wrapped and it stretches to fill available space.

However, if the text block contains small text which is of around 5-10 characters only, then border control does not stretches.

The border control is explicitly set with HorizontalAlignment and VerticalAlignment as Stretch and Margin as 0. but still the border doesn't stretch to what the space is available in column 0?

Upvotes: 0

Views: 3614

Answers (4)

erodewald
erodewald

Reputation: 1825

Please show us your definition of the Border. It is in a Star-sized column but you may have given the Border a HorizontalAlignment or VerticalAlignment which would negate the default behavior to fill the parent's content area. I have verified this example works fine in Kaxaml.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="65"/>
        </Grid.ColumnDefinitions>
        <Border Background="LightGreen"/>
        <TextBlock Grid.Column="1" Foreground="Blue" Text="Column01"/>
        <TextBlock Grid.Column="2" Foreground="Red" Text="Column02"/>
    </Grid>
</Page>

Upvotes: 0

Jared Bienz - MSFT
Jared Bienz - MSFT

Reputation: 3550

I believe your first column cannot be

<ColumnDefinition />

Instead I think it should be

<ColumnDefinition Width="*" />

ColumnDefinition.Width is of type GridLength. GridLength is a structure and it defaults to "Auto". Auto will try to take up the minimal amount of space that its child controls need. A value of "*" means take all space relative to other * columns. (A column of 2* would take up twice as much space as a column of 1*. I usually recommend using numbers between 1 and 100 and thinking of them as percentages). Since no other columns are * columns, a value of simple "*" means take up all remaining space.

You would think that by having all other columns fixed width it would force the first column to be "*", but I don't think that's the case.

You can read more about GridLength here:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.gridlength.aspx

Upvotes: 0

123 456 789 0
123 456 789 0

Reputation: 10865

Try setting the width of the border. If you want to let the border take up the space then set the ColumnDefinition to * (First)

Upvotes: 0

JSJ
JSJ

Reputation: 5691

  <DataGridTemplateColumn Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch"
                                    BorderBrush="Red"
                                    BorderThickness="2">
                                <TextBlock Text="{Binding Name}" />
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Upvotes: -1

Related Questions