[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>
<Page x:Class="ButtonWidthTemplate.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button HorizontalAlignment="Center" VerticalAlignment="Center"
FontSize="48" Padding="20">
Button with Custom Template
<Button.Template>
<ControlTemplate>
<Border Name="border" BorderThickness="3" BorderBrush="Red"
Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
<TextBlock
Name="txtblk"
FontStyle="Italic"
Text="{TemplateBinding Button.Content}"
Margin="{TemplateBinding Button.Padding}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter TargetName="border"
Property="Border.CornerRadius" Value="24" />
<Setter TargetName="border"
Property="Border.BorderThickness" Value="10
" />
<Setter TargetName="txtblk"
Property="TextBlock.FontWeight"
Value="Bold" />
</Trigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="border"
Property="Border.Background"
Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Page>