繁体   English   中英

带模板的嵌套ContentControls

[英]Nested ContentControls with template

我有一个自定义WindowStyle ,XAML如下所示:

    <Style TargetType="{x:Type Window}"
       x:Key="WindowStyle">
    /** Some setters **/
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <AdornerDecorator>
                    <Grid Background="#88000000"
                          x:Name="WindowBackgroundGrid">
                        <Border x:Name="WindowContentBorder"
                                Background="{DynamicResource WindowBackground}"MaxHeight="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}"
                                MaxWidth="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}"
                                Margin="20">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>

                                <!-- Header -->
                                <Border BorderBrush="{DynamicResource BorderBrushColor}"
                                        Background="{DynamicResource PaneHeader_Background}"
                                        Grid.Row="0">
                                    <TextBlock Text="Title"Foreground="{DynamicResource DefaultForeground}"
                                               FontSize="16"
                                               FontWeight="Bold"
                                               Margin="5,5,2,5" />
                                </Border>

                                <!-- Content -->
                                <ScrollViewer Grid.Row="1"
                                              Margin="5">
                                    <ContentPresenter Content="{TemplateBinding Content}" />
                                </ScrollViewer>
                            </Grid>
                        </Border>
                    </Grid>
                </AdornerDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在,我希望内部Grid具有单独的Style以便可以在其他地方使用它。

 <Style x:Key="WindowContentStyle"
       TargetType="{x:Type ContentPresenter}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- Header -->
                        /** Border control **/
                    <!-- Content -->
                    <ScrollViewer Grid.Row="1" 
                                  Margin="5">
                        <ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
                    </ScrollViewer>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

我在WindowStyle中使用ContenPresenter来呈现它:

<ContentPresenter>
    <ContentPresenter.Style>
        <Style TargetType="{x:Type ContentPresenter}"
               BasedOn="{StaticResource WindowContentStyle}" />
    </ContentPresenter.Style>
</ContentPresenter>

问题

上面的编辑没有给我任何错误,但是没有显示我的WindowContentStyle 当我设置Window控件的Content属性并加载样式时

this.window.Content = view;
this.window.Style = (Style)Application.Current.TryFindResource("WindowStyle");

内容显示在WindowStyleContentPresenter中,而不是WindowContentStyle 因此,不使用Template并且没有标题的标题。

如何使外部ContentPresenter传递Content到内部ContentPresenterWindowContentStyle中的那个)?

提前致谢!

问候隆恩

您应该使用ContentControl显示内容, 而不是 ContentPresenter 从MSDN的ContentPresenter页面上:

通常,您可以使用ContentControlControlTemplateContentPresenter来指定将内容添加到的位置。

从MSDN上的ContentControl页面:

ContentControl具有有限的默认样式。 如果要增强控件的外观,可以创建一个新的DataTemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM