[WPF] ResourceTemplate And Button - ControlTemplate !!
WPF 2007. 12. 13. 17:36 |<ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
이 경우 TemplateBinding 표현에서 의존 프로퍼티의 클래스 이름을 앞에 붙일 필요는 없다. 바인딩에는 이 프로퍼티를 참조하라는 정보가 들어 있기 때문.
<Page x:Class="ButtonWidthTemplate.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page2">
<Page.Resources>
<ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
<Border Name="border" BorderThickness="3" BorderBrush="Red"
Background="{TemplateBinding Foreground}">
<TextBlock Name="txtblk"
FontStyle="Italic"
Text="{TemplateBinding Content}"
Margin="{TemplateBinding Padding}"
Foreground="{TemplateBinding Background}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border"
Property="CornerRadius" Value="12" />
<Setter TargetName="txtblk"
Property="FontWeight" Value="Bold" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border"
Property="Background"
Value="{Binding Path=Background}"/>
<Setter TargetName="txtblk"
Property="Foreground"
Value="{Binding Path=Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Page.Resources>
<StackPanel>
<Button Template="{StaticResource btnCustom}"
HorizontalAlignment="Center" Margin="24"
FontSize="24" Padding="10">
Button with Custom Template
</Button>
<Button HorizontalAlignment="Center" Margin="24"
FontSize="24" Padding="10">
Normal Button
</Button>
<Button Template="{StaticResource btnCustom}"
HorizontalAlignment="Center" Margin="24"
FontSize="24" Padding="10">
Another Button with Custom Template
</Button>
</StackPanel>
</Page>