开发者

Why doesn't the contentpresenter fill the grid cell?

Ok I have a contentpresenter inside a grid cell:

        <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>

The contentpresenter finds which UI element to use by the datatemplate defined under resources:

    <DataTemplate DataType="{x:Type ViewModels:RatedValuesViewModel}">
        <Views:RatedValuesView />
    </DataTemplate>

Now, everything works as expected, except for one thing: the View which is placed inside the contentpresenter at runtime does not expand to fill the entire cell. It leaves a big margi开发者_高级运维n on the left and right side.

How can I make the view inside the contentpresenter fill the entire available area?


HorizontalAlign="Stretch" and VerticalAlign="Stretch" are important; however, you also have to remember how Grids work. Grid units are either in absolute pixels, "Auto", which means they size to fit their contents (i.e. minimum size to show everything), or "stars" which means fill up all available space.

Set your second RowDefinition's height to "*". You may also want to set easily-distinguishable border brushes and thicknesses on your grid. Sometimes, it's easy to think X isn't filling up all the available space, when it's really X's container, or X's container's container that isn't filling up the space. Use bright primary colors and large thicknesses (3 or so) and you can tell quickly who's not filling things up.


Chaiguy got it! The View had an explict Width and Height which constrained the view when placed in the cell. Thanks :-)


You must set HorizontalAlign=Stretch, VerticalAlign=Stretch to both Border and Content Presenter to fill the space in grid. and make width=auto.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜