2012-07-10 65 views
0

使用的ContentTemplate我有自定義ContentControl中所謂的TabItem自ContentControl中不會從資源字典

public class TabItem : ContentControl 

我已設置的ContentTemplate到動態資源從資源字典

<TabItem Header="Search" Content="{Binding Path=SearchRibbon}" ContentTemplate="{DynamicResource SearchRibbonTemplate}" /> 

未來使用這種方法我我正在利用MVVM設計模式,並且Content屬性成功綁定到視圖模型...並且程序成功構建並運行,但沒有來自內容模板應該拉入的數據模板內的內容。

<DataTemplate x:Key="SearchRibbonTemplate"> 
    <ItemsControl ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <TabGroupBox Header="{Binding Path=DisplayName}" Width="100"> 
        <TabButton Width="100" Icon="{Binding Path=TemplateResource}" LargeIcon="{Binding Path=TemplateResource}" Command="{Binding Path=Commands}" /> 
       </TabGroupBox> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</DataTemplate> 

如果我按照原樣使用內容控件,則此方法一直有效。爲什麼自定義contentcontrol不能用於ContentTemplate屬性?

乾杯。

編輯

<Style x:Key="RibbonTabItemStyle" TargetType="{x:Type Ribbon_Framework:TabItem}"> 
    <Style.Triggers> 
     <Trigger Property="Group" Value="{x:Null}"> 
      <Setter Property="BorderBrush" Value="{StaticResource RibbonTopBorderBrush}" /> 
     </Trigger> 
     <Trigger Property="Selector.IsSelected" Value="False"> 
      <Setter Property="Foreground" Value="{StaticResource TabItemFontBrush}" /> 
     </Trigger> 
    </Style.Triggers> 
    <Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="VerticalAlignment" Value="Top" /> 
    <Setter Property="Height" Value="31" /> 
    <Setter Property="IsMinimized" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Ribbon_Framework:RibbonTabControl}}, Path=IsMinimized, Mode=OneWay, FallbackValue=False}" /> 
    <Setter Property="IsOpen" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Ribbon_Framework:RibbonTabControl}}, Path=IsDropDownOpen, Mode=OneWay, FallbackValue=True}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Ribbon_Framework:TabItem}"> 
       <Grid x:Name="grid" Background="#00000000" Height="24" VerticalAlignment="Top" Margin="0,7,0,0"> 
        <Border x:Name="border3" Margin="2,0,0,1" BorderThickness="1,0" Grid.ColumnSpan="2"> 
         <Border.BorderBrush> 
          <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
           <GradientStop Color="#CCFFFFFF" Offset="0" /> 
           <GradientStop Offset="1" Color="#7FFFFFFF" /> 
          </LinearGradientBrush> 
         </Border.BorderBrush> 
         <Border x:Name="border4" BorderBrush="{Binding Group.BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" BorderThickness="1,0"> 
          <Border.OpacityMask> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="Black" Offset="0" /> 
            <GradientStop Color="#00000000" Offset="0.981" /> 
           </LinearGradientBrush> 
          </Border.OpacityMask> 
          <Border Background="{Binding Group.Background, RelativeSource={RelativeSource TemplatedParent}}" BorderThickness="0" Margin="0"> 
           <Border.OpacityMask> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="#59000000" Offset="0" /> 
             <GradientStop Color="#00000000" Offset="1" /> 
            </LinearGradientBrush> 
           </Border.OpacityMask> 
           <Rectangle x:Name="rectangle4" Fill="#3FD2D2D2" StrokeThickness="0"> 
            <Rectangle.OpacityMask> 
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
              <GradientStop Color="#7F000000" Offset="0" /> 
              <GradientStop Color="#00000000" Offset="1" /> 
             </LinearGradientBrush> 
            </Rectangle.OpacityMask> 
           </Rectangle> 
          </Border> 
         </Border> 
        </Border> 
        <Grid x:Name="selectedGrid" Visibility="Collapsed" Margin="2,0,0,0" Grid.ColumnSpan="1"> 
         <Rectangle x:Name="rectangle" Fill="{StaticResource ActiveTabBackgroundBrush}" Stroke="{x:Null}" StrokeThickness="0" Margin="0" VerticalAlignment="Bottom" Height="1" /> 
         <Rectangle x:Name="rectangle1" Fill="{StaticResource ActiveTabBackgroundBrush}" Stroke="{x:Null}" StrokeThickness="0" Margin="1.8,0" VerticalAlignment="Bottom" Height="1.2" /> 
         <Rectangle x:Name="rectangle2" Fill="{StaticResource ActiveTabBackgroundBrush}" Stroke="{x:Null}" StrokeThickness="0" Margin="2,0" VerticalAlignment="Bottom" Height="2" /> 
         <Border x:Name="border" Margin="1,0,1,2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{StaticResource ActiveTabBackgroundBrush}"> 
          <Border x:Name="border_Copy3" Margin="0" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{x:Null}" BorderBrush="{StaticResource ActiveTabBackgroundBrush}"> 
           <Border x:Name="border_Copy4" Margin="0,0,-2,-1" BorderThickness="0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{Binding Group.Background, RelativeSource={RelativeSource TemplatedParent}}" BorderBrush="{x:Null}"> 
            <Border.OpacityMask> 
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
              <GradientStop Color="#3F000000" Offset="0" /> 
              <GradientStop Color="#00000000" Offset="0.59" /> 
             </LinearGradientBrush> 
            </Border.OpacityMask> 
           </Border> 
          </Border> 
         </Border> 
         <Border x:Name="border1" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Bottom" Width="2" Height="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,1,1" CornerRadius="0,0,2,0" /> 
         <Border x:Name="border2" HorizontalAlignment="Right" Margin="0" VerticalAlignment="Bottom" Width="2" Height="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,0,0,1" CornerRadius="0,0,0,2" /> 
         <Rectangle x:Name="rectangle3" StrokeThickness="0" Margin="7,-7,7,0" VerticalAlignment="Top" Height="14" IsHitTestVisible="False"> 
          <Rectangle.Fill> 
           <RadialGradientBrush Center="0.498,0.501" GradientOrigin="0.498,0.501" RadiusY="0.464"> 
            <GradientStop Color="#7FFFFFFF" Offset="0" /> 
            <GradientStop Offset="1" /> 
           </RadialGradientBrush> 
          </Rectangle.Fill> 
         </Rectangle> 
        </Grid> 
        <Grid x:Name="hoverGrid" Margin="2,0,0,0" Visibility="Collapsed" Grid.ColumnSpan="1"> 
         <Border x:Name="border_Copy" Margin="1,0,1,1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{x:Null}"> 
          <Border.OpacityMask> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="#CC000000" Offset="0" /> 
            <GradientStop Color="#F7FFFFFF" Offset="0.985" /> 
           </LinearGradientBrush> 
          </Border.OpacityMask> 
          <Border x:Name="border_Copy1" Margin="0" BorderBrush="{StaticResource ActiveTabBackgroundBrush}" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{x:Null}"> 
           <Border.OpacityMask> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="#CCFFFFFF" Offset="0" /> 
             <GradientStop Color="#F7FFFFFF" Offset="1" /> 
            </LinearGradientBrush> 
           </Border.OpacityMask> 
           <Border x:Name="border_Copy2" Margin="0" BorderBrush="{x:Null}" BorderThickness="0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{StaticResource ActiveTabBackgroundBrush}"> 
            <Border.OpacityMask> 
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
              <GradientStop Color="#7FFFFFFF" Offset="0.004" /> 
              <GradientStop Offset="0.5" /> 
             </LinearGradientBrush> 
            </Border.OpacityMask> 
           </Border> 
          </Border> 
         </Border> 
        </Grid> 
        <Grid x:Name="focusedGrid" Margin="2,0,0,0" Visibility="Collapsed" Grid.ColumnSpan="1"> 
         <Grid.OpacityMask> 
          <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
           <GradientStop Color="White" Offset="0" /> 
           <GradientStop Offset="1" Color="#19FFFFFF" /> 
          </LinearGradientBrush> 
         </Grid.OpacityMask> 
         <Border x:Name="border_Copy5" Margin="1,0,1,1" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{StaticResource ButtonHoverOuterBackgroundBrush}" BorderBrush="{StaticResource ButtonHoverOuterBorderBrush}"> 
          <Border x:Name="border_Copy6" Margin="0" BorderBrush="{StaticResource ButtonHoverInnerBorderBrush}" BorderThickness="1,1,1,0" VerticalAlignment="Stretch" CornerRadius="2,2,0,0" Background="{StaticResource ButtonHoverInnerBackgroundBrush}" /> 
         </Border> 
        </Grid> 
        <Border x:Name="PART_ContentContainer" HorizontalAlignment="Stretch" Margin="6,2,4,2" VerticalAlignment="Stretch" Padding="15,0,0,0" Grid.ColumnSpan="1"> 
         <Label x:Name="contentPresenter" AutomationProperties.Name="{TemplateBinding Header}" AutomationProperties.AutomationId="{TemplateBinding Name}" Content="{TemplateBinding Header}" Margin="0, -3, 0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Height="Auto" /> 
        </Border> 
        <Grid x:Name="separatorGrid" Margin="0" Visibility="Collapsed" HorizontalAlignment="Right" Width="2" Grid.Column="1"> 
         <Rectangle x:Name="separatorRectangle1" Fill="{StaticResource RibbonTopBorderBrush}" Stroke="{StaticResource RibbonTopBorderBrush}" Margin="0,0,1,0" HorizontalAlignment="Right" Width="1" Opacity="1" Visibility="Visible"> 
          <Rectangle.OpacityMask> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="#00000000" Offset="0" /> 
            <GradientStop Color="#E5FFFFFF" Offset="0.382" /> 
           </LinearGradientBrush> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
         <Rectangle x:Name="separatorRectangle2" Fill="{StaticResource ActiveTabBackgroundBrush}" Stroke="{StaticResource ActiveTabBackgroundBrush}" HorizontalAlignment="Right" Margin="0" Width="1" Opacity="1" Visibility="Visible"> 
          <Rectangle.OpacityMask> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="#00000000" Offset="0" /> 
            <GradientStop Color="#BFFFFFFF" Offset="0.329" /> 
           </LinearGradientBrush> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
        </Grid> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Opacity" TargetName="contentPresenter" Value="0.5" /> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="HasRightGroupBorder" Value="False" /> 
          <Condition Property="HasLeftGroupBorder" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="BorderThickness" TargetName="border3" Value="1,0,0,0" /> 
         <Setter Property="BorderThickness" TargetName="border4" Value="1,0,0,0" /> 
         <Setter Property="Margin" TargetName="rectangle4" Value="0" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="HasLeftGroupBorder" Value="False" /> 
          <Condition Property="HasRightGroupBorder" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="BorderThickness" TargetName="border4" Value="0,0,1,0" /> 
         <Setter Property="BorderThickness" TargetName="border3" Value="0,0,1,0" /> 
         <Setter Property="Margin" TargetName="rectangle4" Value="0" /> 
         <Setter Property="Margin" TargetName="border3" Value="0,0,0,1" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="HasLeftGroupBorder" Value="False" /> 
          <Condition Property="HasRightGroupBorder" Value="False" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="BorderThickness" TargetName="border4" Value="0,0,0,0" /> 
         <Setter Property="BorderThickness" TargetName="border3" Value="0,0,0,0" /> 
         <Setter Property="Margin" TargetName="rectangle4" Value="0" /> 
         <Setter Property="Margin" TargetName="border3" Value="0,0,0,1" /> 
        </MultiTrigger> 
        <Trigger Property="Group" Value="{x:Null}"> 
         <Setter Property="Visibility" TargetName="border3" Value="Collapsed" /> 
         <Setter Property="Margin" TargetName="separatorGrid" Value="-1,0,0,1" /> 
         <Setter Property="Visibility" TargetName="border_Copy3" Value="Collapsed" /> 
         <Setter Property="Visibility" TargetName="rectangle3" Value="Collapsed" /> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="Selector.IsSelected" Value="True" /> 
          <Condition Property="IsMinimized" Value="False" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="Visibility" TargetName="selectedGrid" Value="Visible" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
          <Condition Property="Selector.IsSelected" Value="False" /> 
          <Condition Property="IsMinimized" Value="False" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="Visibility" TargetName="hoverGrid" Value="Visible" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
          <Condition Property="IsMinimized" Value="True" /> 
          <Condition Property="IsOpen" Value="False" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="Visibility" TargetName="hoverGrid" Value="Visible" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="Selector.IsSelected" Value="True" /> 
          <Condition Property="IsMinimized" Value="True" /> 
          <Condition Property="IsOpen" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="Visibility" TargetName="hoverGrid" Value="Visible" /> 
         <Setter Property="Background" TargetName="grid" Value="#00000000" /> 
        </MultiTrigger> 
        <Trigger Property="IsSeparatorVisible" Value="True"> 
         <Setter Property="Visibility" TargetName="separatorGrid" Value="Visible" /> 
         <Setter Property="Margin" TargetName="PART_ContentContainer" Value="6,2" /> 
         <Setter Property="Margin" TargetName="hoverGrid" Value="2,0" /> 
         <Setter Property="Margin" TargetName="selectedGrid" Value="2,0" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="IsTabStop" Value="False" /> 
    <Setter Property="Indent" Value="12" /> 
    <Setter Property="Foreground" Value="{StaticResource TabItemSelectedFontBrush}" /> 
    <Setter Property="FontFamily" Value="{StaticResource DefaultFontFamily}" /> 
    <Setter Property="FontSize" Value="{StaticResource DefaultFontSize}" /> 
    <Setter Property="Margin" Value="0" /> 
    <Setter Property="BorderBrush" Value="{Binding Group.BorderBrush, RelativeSource={RelativeSource Self}}" /> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ControlStyleEmptyFocus}" /> 
</Style> 
+0

您確定使用名爲'TabItem'的自定義項目而不是內置的'TabItem'控件嗎?標籤應該看起來像''。另外,什麼是'SearchRibbon'?如果它不是一個可枚舉的集合,那很有可能你的ItemsControl沒有找到任何要渲染的項目,所以最終的結果是什麼都不顯示。 – Rachel 2012-07-10 14:30:04

+0

您綁定到內容的對象的類型是什麼? – 2012-07-10 14:30:27

+0

從技術上講,您不必爲您的自定義命名空間@Rachel設置自定義名稱;) – Kolky 2012-07-10 14:32:12

回答

0

與該建流利絲帶庫,我改造的開發商一一對應後,他指出,爲了實現這樣的設計會花費更多的然後就修改ContentPresenter和一個完整的返工會必要。在這種情況下,他曾建議建立一個ViewModels列表,它可以提供我期待的設計。

感謝大家的回答,但困難的方式似乎是這種情況下的簡單方法。

乾杯。

0

我的猜測是,你沒有定義默認樣式在Themes/Generic.xaml控制應控制的Template設定爲至少包含一個ContentPresenter

例如

<Style TargetType="local:TabItem"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:TabItem"> 
       <!-- Usually there is also a Border which template binds the Margin, Padding, etc. --> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

我有一個ControlTemplate的樣式的模板屬性的值...你說這應該是一個ContentPresenter,而不是? – bl4kh4k 2012-07-10 14:42:08

+1

@ bl4kh4k:我說ControlTemplate需要包含一個ContentPresenter,否則內容將不會顯示。此外,ControlTemplate上的TargetType需要使ContentPresenter綁定到相應的屬性。 – 2012-07-10 14:44:35

+0

嗯,所以我確實在ControlTemplate中有邊框和網格,所以我應該在ControlTemplate的結束標記之前放置?我不明白爲什麼這個工作? – bl4kh4k 2012-07-10 14:46:20