سلام بر تمامی برنامه نویسان سی شارپ 😉 در این مطلب آموزش استفاده از ControlTemplate با wpf و c#.net را آماده کردم. امیدوارم که این کد برای شما مفید باشد.
تصویر خروجی این کد:
کد استفاده از ControlTemplate با wpf و c#.net :
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
<Border Name="border" BorderThickness="3" BorderBrush="Blue"
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}" />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
<Border Name="border" BorderThickness="3" BorderBrush="Blue"
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}" />