为什么contentpresenter不填充网格单元?

Mar*_*ius 11 wpf

好的,我在网格单元格中有一个contentpresenter:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <WrapPanel Grid.Row="0" Grid.Column="0">
                <RadioButton GroupName="a" IsChecked="{Binding Path=SpecifyRatedValues, Mode=TwoWay}">Specify</RadioButton>
                <RadioButton GroupName="b" IsChecked="{Binding Path=SpecifyRatedValues, Converter={StaticResource invertBoolean}}">Auto generate</RadioButton>
            </WrapPanel>

            <Border BorderBrush="Black" BorderThickness="3" Grid.Row="1" Grid.Column="0">
                <ContentPresenter Content="{Binding Path=RatedValues}"></ContentPresenter>
            </Border>                    
        </Grid>
Run Code Online (Sandbox Code Playgroud)

contentpresenter查找资源下定义的datatemplate要使用的UI元素:

    <DataTemplate DataType="{x:Type ViewModels:RatedValuesViewModel}">
        <Views:RatedValuesView />
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

现在,一切都按预期工作,除了一件事:在运行时放置在contentpresenter内部的视图不会扩展以填充整个单元格.它在左侧和右侧留下了很大的余量.

如何使contentpresenter中的视图填充整个可用区域?

FMM*_*FMM 13

Horizo​​ntalAlign ="Stretch"和VerticalAlign ="Stretch"很重要; 但是,您还必须记住网格的工作原理.网格单位是绝对像素,"自动",这意味着它们的大小适合其内容(即显示所有内容的最小尺寸),或"星星",这意味着填满所有可用空间.

将第二个RowDefinition的高度设置为"*".您可能还想在网格上设置易于区分的边框画笔和粗细.有时,很容易认为X没有填满所有可用空间,当它真的是X的容器,或X的容器容器没有填满空间时.使用明亮的原色和大的厚度(3左右),你可以很快地告诉谁没有填补.


Mar*_*ius 3

柴吉明白了!视图具有显式的宽度和高度,当放置在单元格中时会限制视图。谢谢 :-)