这个方法在Window内使用是没有问题的。代码如下:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="Yellow">
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="200"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="text1" Text="111" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18"/>
<TextBlock x:Name="text2" Text="222" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" Grid.Column="1"/>
<TextBlock x:Name="text3" Text="333" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" Grid.Row="1"/>
<TextBlock x:Name="text4" Text="444" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" Grid.Row="1" Grid.Column="1"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF8AABF8"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="RecFill" Color="Red"></SolidColorBrush>
<SolidColorBrush x:Key="RecStroke" Color="LightBlue"></SolidColorBrush>
<Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Black"></Setter>
<Setter Property="Background" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse x:Name="Rec" Width="18" Height="18" HorizontalAlignment="Center" VerticalAlignment="Center"></Ellipse>
<TextBlock x:Name="text" Text="×" FontWeight="Bold" Margin="0,0,0,3" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="Rec" Value="{StaticResource RecFill}"/>
<Setter Property="Foreground" TargetName="text" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="25"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="Background" Value="#2347BF"/>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Padding" Value="5,0,0,0"></Setter>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Height" Value="30"/>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="BorderThickness" Value="1,1,4,1"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Border" CornerRadius="10" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="5,0,1,5" Background="{TemplateBinding Background}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="25"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="TabItemContent" Grid.Column="0" ContentSource="Header" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"></ContentPresenter>
<Button x:Name ="btn_CloseItem" Grid.Column="1" Style="{StaticResource CloseButtonStyle}" Click="btn_Close_Click"></Button>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter TargetName="Border" Property="Background" Value="white"/>
<Setter Property="TextElement.Foreground" Value="Black" TargetName="TabItemContent"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TabControl}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="61*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="1" BorderThickness="2,0,2,2" CornerRadius="15" Background="Transparent" Grid.ColumnSpan="2" Margin="5 0 5 0">
<ContentPresenter ContentSource="SelectedContent" />
</Border>
<StackPanel Orientation="Horizontal" Grid.Row="0" Panel.ZIndex="1" IsItemsHost="true" Grid.ColumnSpan="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Button x:Name="btn" Click="Button_Click"/>
</Window>
后台代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBlock text = this.btn.Template.FindName("text1", this.btn) as TextBlock;//查找与此模板中定义的指定名称关联的元素
text.Text = "One";
Grid grid = text.Parent as Grid;//获取模板内(text)控件的逻辑父元素
(grid.Children[1] as TextBlock).Text = "Two";
(grid.Children[2] as TextBlock).Text = "Three";
(grid.Children[3] as TextBlock).Text = "Four";
}
显示结果:
可是如果这个方法在UserControl中使用,是找不到模板中的控件的。
//
// 摘要:
// 查找与此模板中定义的指定名称关联的元素。
//
// 参数:
// name:
// 字符串名称。
//
// templatedParent:
// 应用此模板的 System.Windows.FrameworkElement 的上下文。
//
// 返回结果:
// 与指定名称关联的元素。
public object FindName(string name, FrameworkElement templatedParent);
原因是控件初始化时还未生成,如果想占用这种方法设置ControlTtemplate中的属性。要把它放到Load方法中。这么使用就可以了。
函数中已找到模板中的Button,这样就可以后台修改属性了。