2014-05-19 1 views
0

저는 WPF를 처음 사용하기 때문에 TabControl을 템플릿으로 작성하여 학습하기로 결정했습니다. 내 템플릿에 <TabControl BorderBrush="Red">을 인식하려면 {TemplateBinding BorderBrush}을 사용해야했습니다.기본 ControlTemplates를 사용하면 TemplateBinding없이 사용자 정의 할 수 있습니까?

그것은 여기 리소스 사용하고
<Border x:Name="Border" 
     Grid.Row="1" 
     BorderThickness="1" 
     CornerRadius="2" 
     KeyboardNavigation.TabNavigation="Local" 
     KeyboardNavigation.DirectionalNavigation="Contained" 
     KeyboardNavigation.TabIndex="2"> 
    <Border.Background> 
     <LinearGradientBrush EndPoint="0.5,1" 
          StartPoint="0.5,0"> 
      <GradientStop Color="{DynamicResource ContentAreaColorLight}" 
          Offset="0" /> 
      <GradientStop Color="{DynamicResource ContentAreaColorDark}" 
          Offset="1" /> 
     </LinearGradientBrush> 
    </Border.Background> 
    <Border.BorderBrush> 
     <SolidColorBrush Color="{DynamicResource BorderMediumColor}"/> 
    </Border.BorderBrush> 
    <ContentPresenter x:Name="PART_SelectedContentHost" 
         Margin="4" 
         ContentSource="SelectedContent" /> 
</Border> 

:

<Color x:Key="BorderMediumColor">#FF888888</Color> 

에는 바인딩이 없습니다 아무 곳이나 아직

<ControlTemplate TargetType="TabControl"> 
    <Grid Name="grid" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
     <TabPanel Grid.Row="0" IsItemsHost="True"></TabPanel> 
     <Border Grid.Row="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"></Border> 
    </Grid> 
</ControlTemplate> 

내가 이것을 볼 TabControl Styles and Templates에서 기본 템플릿을 보면 : 이것은 내가 무엇을 가지고 여전히 BorderBrush를 인식합니다. 이 사실을 이해하기 위해 내가 놓친 중요한 지식은 무엇입니까?

+0

당신은 사과와 오렌지를 비교하고있는 것처럼 보입니다. 귀하의 코드는 실제 테두리 브러시를 특정 값으로 설정합니다. 샘플은 경계 브러시의 색상 속성을 특정 값으로 설정합니다. Border 블록을 여러 줄로 확장하고 샘플에서 테두리 브러시 블록을 삽입하십시오. – tinstaafl

+1

MSDN의 템플릿이 불완전하다는 것을 알았습니다. 템플릿을 추출하려면 'Blend'와 같은 도구를 사용해야합니다. http://stackoverflow.com/a/1560072/62195 –

+0

@RyanEmerle 맞습니다. 내가 사용하고 있던 링크에서 전체 템플릿을 복사했는데 기본값이 아무 것도 없었습니다. 나는 실제 템플릿을 추출했고 훨씬 더 의미가 있습니다. – TreeTree

답변

0

템플릿이 TabControl Styles and Templates에서 기본 TabControl 템플릿이 아닙니다. 나는 Right click on TabControl in the Designer > Edit Template > Edit a Copy...하여 기본 템플릿을 추출하고이 내가 가진 것입니다 :

<SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/> 
<SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/> 
<Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}"> 
    <Setter Property="Padding" Value="2"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TabControl}"> 
       <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition x:Name="ColumnDefinition0"/> 
         <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition x:Name="RowDefinition0" Height="Auto"/> 
         <RowDefinition x:Name="RowDefinition1" Height="*"/> 
        </Grid.RowDefinitions> 
        <TabPanel x:Name="headerPanel" Background="Transparent" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/> 
        <Border x:Name="contentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local"> 
         <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="TabStripPlacement" Value="Bottom"> 
         <Setter Property="Grid.Row" TargetName="headerPanel" Value="1"/> 
         <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/> 
         <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
         <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/> 
         <Setter Property="Margin" TargetName="headerPanel" Value="2,0,2,2"/> 
        </Trigger> 
        <Trigger Property="TabStripPlacement" Value="Left"> 
         <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/> 
         <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/> 
         <Setter Property="Grid.Column" TargetName="headerPanel" Value="0"/> 
         <Setter Property="Grid.Column" TargetName="contentPanel" Value="1"/> 
         <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/> 
         <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/> 
         <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
         <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> 
         <Setter Property="Margin" TargetName="headerPanel" Value="2,2,0,2"/> 
        </Trigger> 
        <Trigger Property="TabStripPlacement" Value="Right"> 
         <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/> 
         <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/> 
         <Setter Property="Grid.Column" TargetName="headerPanel" Value="1"/> 
         <Setter Property="Grid.Column" TargetName="contentPanel" Value="0"/> 
         <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/> 
         <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/> 
         <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
         <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> 
         <Setter Property="Margin" TargetName="headerPanel" Value="0,2,2,2"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

포함하는 :

<Border x:Name="contentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local"> 
    <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
</Border> 

보라 TemplateBindings을! 나는 더 이상 혼란 스럽다.

관련 문제