[英]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");
内容显示在WindowStyle的ContentPresenter
中,而不是WindowContentStyle 。 因此,不使用Template
并且没有标题的标题。
如何使外部ContentPresenter
传递Content
到内部ContentPresenter
( WindowContentStyle中的那个)?
提前致谢!
问候隆恩
您应该使用ContentControl
显示内容, 而不是 ContentPresenter
。 从MSDN的ContentPresenter
类页面上:
通常,您可以使用
ContentControl
的ControlTemplate
中的ContentPresenter
来指定将内容添加到的位置。
从MSDN上的ContentControl
类页面:
ContentControl
具有有限的默认样式。 如果要增强控件的外观,可以创建一个新的DataTemplate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.