user2189143
user2189143

Reputation:

XAML width setting as percentage

I have the following code.

<Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100*" />
                    <ColumnDefinition Width="100*" />
                    <ColumnDefinition Width="100*" />
                    <ColumnDefinition Width="100*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="100*" />
                    <RowDefinition Height="100*" />
                    <RowDefinition Height="100*" />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Grid.Column="0" Width="30*">
                    ...
                </StackPanel>
     </Grid>

I am trying to set Stackpanel width to 30% of the cell. I already have referred the post WPF: Setting the Width (and Height) as a Percentage Value . As per the post It should have worked the width as 30* as it is with in the grid. But It is showing the error "30* string cannot be converted to length". So I would like to rectify the error or I just want to get a way to set the stackpanel of width 30% of cell. Thanks.

Upvotes: 0

Views: 8672

Answers (1)

nuro
nuro

Reputation: 258

Width for the stackpanel cant anything but a double. Grid width can take * and auto as well (* being default if not specified)

Also note that a StackPanel will never take up more width then whatever the content requires. HorizontalAlignment="Stretch" have no effect.

I suggest you do this instead.

        <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="100*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100*" />
            <RowDefinition Height="100*" />
            <RowDefinition Height="100*" />
        </Grid.RowDefinitions>


        <Grid Grid.Row="0" Grid.Column="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <StackPanel>
            </StackPanel>

        </Grid>
    </Grid>

Upvotes: 5

Related Questions