ContentPresenter 布局不正确

ContentPresenter not lay-outing properly

我正在开发一个通用应用程序(XAML 和 C# 组合),我已经创建了一个自定义控件,它应该像一种卡片视图一样工作。翻转卡片会显示其他内容。为此,我使用两个 ContentPresenters 和一些动画(视觉状态)在需要时旋转 ContentPresenters,以便始终只有一个可见。

动画效果很好,但是,我遇到了布局问题,ContentPresenter 的 DataTemplate 中的 TextBlock 没有拉伸到控件的整个宽度。尝试通过创建一个单独的 ContentPresenter 并添加完全相同的内容来重现此行为,效果很好。一定是我的控件样式有问题,但我没看出我做错了什么。

您可以在下面看到我使用的样式(为澄清起见,我省略了视觉状态,因为我认为这不是重点。)

<Style TargetType="controls:FlipCard">
    <Setter Property="Padding" Value="0" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:FlipCard">
                <Grid x:Name="Root" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Grid x:Name="FrontContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.Projection>
                            <PlaneProjection RotationX="0" RotationY="0" />
                        </Grid.Projection>
                        <ContentPresenter x:Name="Front" ContentTemplate="{TemplateBinding Front}" FontWeight="Light" />
                    </Grid>
                    <Grid x:Name="BackContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.Projection>
                            <PlaneProjection RotationX="0" RotationY="0" />
                        </Grid.Projection>
                        <ContentPresenter x:Name="Back" ContentTemplate="{TemplateBinding Back}" />
                    </Grid>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup>
                            <!-- Rotate around X-axis -->
                            <VisualState x:Name="FlipForwardShowBack">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="FrontContainer">
                                        <SplineDoubleKeyFrame KeyTime="0" Value="0"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="90"/>
                                        <SplineDoubleKeyFrame KeyTime="0:0:0.8" Value="90"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="BackContainer">
                                        <SplineDoubleKeyFrame KeyTime="0" Value="-90"/>
                                        <SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="-90"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="0"/>
                                    </DoubleAnimationUsingKeyFrames>

                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.FontFamily)" Storyboard.TargetName="Front">
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <FontFamily>Global User Interface</FontFamily>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="FlipForwardShowFront">
                                ...
                            </VisualState>

                            ...
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果有人能帮助我,我将不胜感激!谢谢! :) PS:如果您想实际查看问题,可以在此处下载示例项目:http://1drv.ms/1BdRPQ0

我不知道它背后的逻辑,但我看不出有什么理由要覆盖 MeasureOverride

只需注释掉整个 protected override Size MeasureOverride(Size availableSize) 即可。