5-WPFTemplates-And-CustomEl.../5-17.3创建控件模板/MainWindow.xaml

22 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

2025-01-23 22:46:48 +08:00
<Window x:Class="_5_17._3创建控件模板.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<!-- TargetType="{x:Type Button}" 目标对象是按钮 -->
<ControlTemplate x:Key="GridControlTemplate" TargetType="{x:Type Button}">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red"
TextBlock.Foreground="White">
<!-- ContentPresenter 在此插入内容RecognizesAccessKey 不是必须的但是可以使用组合键可以触发 -->
<!-- Margin="{TemplateBinding Padding}" 如果不加 设置Padding无效 -->
<ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<Button Template="{StaticResource GridControlTemplate}" Margin="15" Padding="15">按钮</Button>
</StackPanel>
</Window>