内容控制内容调整以填充Silverlight 4

时间:2010-09-14 07:05:10

标签: silverlight contentcontrol

我有一个ContentControl的样式,我想在我目前有边框的地方使用它。当我使用它时,子控件不会伸展到填充,只占用少量空间。我已经尝试将Horizo​​ntalAlignment =“Stretch”应用于所有内容,但它不起作用。怎么了?

<Style x:Key="GradientPanel" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    <Rectangle RadiusY="10" RadiusX="10" Stroke="Black" StrokeThickness="0">
                        <Rectangle.Effect>
                            <DropShadowEffect Opacity="0.56" ShadowDepth="1" BlurRadius="3" />
                        </Rectangle.Effect>
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FFE1EAF3"/>
                                <GradientStop Color="White" Offset="1"/>
                                <GradientStop Color="#FFFAFBFD" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <ContentPresenter Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

...

之前(工作正常):

<Border Style="{StaticResource SearchContainerBorder}" >
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ToggleButton  Style="{StaticResource ToggleButtonExpanderStyle}" Grid.Row="0" Grid.Column="1" Height="25" Width ="25" HorizontalAlignment="Center" VerticalAlignment="Top" />
        <ContentControl Grid.Row="0"  HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Border>

之后(用ContentControl替换Border):

<ContentControl Style="{StaticResource GradPanel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ToggleButton  Style="{StaticResource ToggleButtonExpanderStyle}" Grid.Row="0" Grid.Column="1" Height="25" Width ="25" HorizontalAlignment="Center" VerticalAlignment="Top" />
        <ContentControl Grid.Row="0"  HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</ContentControl>

1 个答案:

答案 0 :(得分:45)

您需要在外部ContentControl上设置HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"设置。

默认行为是 NOT 拉伸容器的内容。

e.g。第一行应该是:

<ContentControl Style="{StaticResource GradPanel}" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" 
                HorizontalContentAlignment="Stretch" 
                VerticalContentAlignment="Stretch" >