1.在控件声明中直接定义;
- <Button Content="Button1">
- <Button.Template>
- <ControlTemplate TargetType=“Button”>
- <!--在此ControlTemplate‐‐>
- </ControlTemplate>
- </Button.Template>
- </Button>
2.在容器的资源属性中定义:
- <StackPanel>
- <StackPanel.Resources>
- <Style x:Key="ButtonStyle" TargetType="Button">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="Button">
- <!‐‐在此定义ControlTemplate ‐‐>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </StackPanel.Resources>
- <Button Style="{StaticResource ButtonStyle}" Content="Button1"/>
- </StackPanel>
3.app.xaml文件中定义:
- <Application.Resources>
- <Style TargetType="TextBlock" x:Key="TextBlockPrompt">
- <Setter Property="VerticalAlignment" Value="Bottom" />
- <Setter Property="HorizontalAlignment" Value="Left" />
- <Setter Property="FontFamily" Value="Verdana" />
- <Setter Property="FontSize" Value="14" />
- <Setter Property="FontWeight" Value="Medium" />
- <Setter Property="Foreground" Value="Blue" />
- </Style>
- </Application.Resources>
前段绑定方法:
- <Button Style="{StaticResource TextBlockPrompt}" Content="Button1"/>