首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有TemplateBinding的情况下,默认的ControlTemplates如何让你进行自定义?

在没有TemplateBinding的情况下,默认的ControlTemplates如何让你进行自定义?
EN

Stack Overflow用户
提问于 2014-05-20 00:22:13
回答 1查看 184关注 0票数 0

我是第一次接触WPF,所以我决定通过模板化TabControl来学习。我不得不在我的模板中使用{TemplateBinding BorderBrush}来识别<TabControl BorderBrush="Red">。这就是我所拥有的:

代码语言:javascript
复制
<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的默认模板时,我看到了以下内容:

代码语言:javascript
复制
<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>

它正在使用这里的资源:

代码语言:javascript
复制
<Color x:Key="BorderMediumColor">#FF888888</Color>

没有任何绑定,但它仍然可以识别BorderBrush。要理解这一点,我错过了哪些重要的知识?

EN

回答 1

Stack Overflow用户

发布于 2014-05-20 02:05:32

事实证明,TabControl Styles and Templates中的模板不是默认的选项卡控件模板。我提取了Right click on TabControl in the Designer > Edit Template > Edit a Copy...的默认模板,得到的结果如下:

代码语言:javascript
复制
<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>

它包含:

代码语言:javascript
复制
<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!我不再困惑了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23742687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档