1、普通控件重写需要添加一行
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}"></ContentPresenter>
2、TextBox之类的编辑控件重写,需要填加一行
<ScrollViewer x:Name="PART_ContentHost"></ScrollViewer>
只有这样才能进入编辑模式
举个栗子:
<ControlTemplate x:Key="RoundTextBoxTemplate" TargetType="TextBox"> <Border CornerRadius="5" BorderBrush="LightSkyBlue" BorderThickness="2" Background="{TemplateBinding Background}" Margin="3"> <ScrollViewer x:Name="PART_ContentHost"></ScrollViewer> </Border> </ControlTemplate> <Style x:Key="RoundTextBoxStyle" TargetType="TextBox"> <Setter Property="Template" Value="{DynamicResource ResourceKey=RoundTextBoxTemplate}" ></Setter> </Style>
上面就是实现最简单的TextBox重写样式的功能了