XAML宽度设置为百分比

0 c# xaml width

我有以下代码.

<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>
Run Code Online (Sandbox Code Playgroud)

我试图将Stackpanel宽度设置为单元格的30%.我已经提到了WPF的帖子:将宽度(和高度)设置为百分比值.按照帖子它应该在网格中将宽度设置为30*.但它显示错误"30*字符串无法转换为长度".所以我想纠正错误,或者我只想找到一种方法来设置宽度为30%的单元格的堆栈面板.谢谢.

nur*_*uro 5

stackpanel的宽度除了双倍之外什么都不能.网格宽度也可以采用*和auto(如果未指定,则为默认值)

另请注意,无论内容需要什么,StackPanel都不会占用更多宽度.Horizo​​ntalAlignment ="拉伸"无效.

我建议你这样做.

        <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>
Run Code Online (Sandbox Code Playgroud)