2014-10-10 108 views
0

我有一个自定义WindowStyle,在XAML看起来是这样的:嵌套ContentControls与模板

<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> 

现在我想在一个单独的StyleGrid,这样我可以在其他地方使用它。

<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> 

而且我用的是ContenPresenter在我WindowStyle呈现它:

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

问题

编辑上面没有给我任何错误,但它不” t目前我的WindowContentStyle。 当我设置Window控制的Content属性并加载样式

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

内容被显示在ContentPresenterWindowStyle,而不是在WindowContentStyle。因此,Template没有使用,我没有标题的标题。

我怎样才能让我的外ContentPresenter通上Content到我的内心ContentPresenter(在WindowContentStyle之一)?

在此先感谢!

问候 Loetn

回答

1

您应该使用ContentControl显示您的内容,一个ContentPresenter。从ContentPresenter Class页面上MSDN:

您通常使用ContentPresenter中的ContentControlControlTemplate到指定的内容将被添加。

ContentControl Class页面上MSDN:

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

+0

感谢您的建议。我编辑了我的外部'ContentPresenter'到'ContentControl'(或者你的意思是内部的'ContentPresenter'?)。它确实显示我的标题,但不显示任何内容。任何想法如何设置*内容绑定*? – Loetn 2014-10-10 09:45:57

+0

我的意思是你应该只使用一个'ContentPresenter'来呈现内容。您不应该尝试对该内容进行“样式”设置,因为“样式”选项有限,无论如何都不会传递给实际内容。你应该在所有其他情况下使用ContentControl,你可以使用ContentControl.Content属性来设置内容和ContentControl.ContentTemplate属性(http://msdn.microsoft.com/)com/en-us/library/system.windows.controls.contentcontrol.contenttemplate(v = vs.110).aspx)来应用你的'DataTemplate'。 – Sheridan 2014-10-10 10:54:44