Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the contentpresenter fill the grid cell?

Tags:

wpf

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 margin on the left and right side.

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

like image 572
Marius Avatar asked Jul 28 '10 14:07

Marius


1 Answers

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.

like image 128
FMM Avatar answered Oct 28 '22 01:10

FMM