110,571
社区成员
发帖
与我相关
我的任务
分享
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="Blue">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Margin="10" Background="Red">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
public class CustomControl1 : Button
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Border border = this.ContentTemplate.LoadContent() as Border;
border.Background = new SolidColorBrush(Colors.Yellow);
}
}
<Window x:Class="WPF5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WPF5"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomControl1 Content="张三"/>
</Grid>
</Window>