==WPF コントロール==
[[WPF][Silverlight][.Net][Universal Windows Platform]]
{{amazon|4798114200}}
*コンテンツコントロールはControlから派生し、Templateという共通プロパティを継承
*コンテンツに影響を与えずに、外観を変更したい場合新しいテンプレートを定義出来る
<<Button>> <<Button.Template>> <<ControlTemplate TargetType="{x:Type Button}">> <<Rectangle Fill="Red" Width="75" Height="23"/>> <</ControlTemplate>> <</Button.Template>>
MyButton
<</Button>>
[[File:1447_wpf_template.jpg]]
*コンテンツプレゼンタを含めることでボタンらしい外観のボタンを作成できる
*コンテンツプレゼンタは規定ではテンプレートが適用されるコントロールのContentプロパティの値を表示する
<<StackPanel Name="MainPanel" Initialized="MainPanel_Initialized" >> <<StackPanel.Resources>> <<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">> <<Border CornerRadius="6" BorderThickness="4">> <<Border.BorderBrush>> <<LinearGradientBrush EndPoint="0,1">> <<LinearGradientBrush.GradientStops>> <<GradientStop Offset="0" Color="White"/>> <<GradientStop Offset="1" Color="Green"/>> <</LinearGradientBrush.GradientStops>> <</LinearGradientBrush>> <</Border.BorderBrush>> <<Border.Background>> <<LinearGradientBrush EndPoint="0,1">> <<LinearGradientBrush.GradientStops>> <<GradientStop Offset="0" Color="Green"/>> <<GradientStop Offset="1" Color="White"/>> <</LinearGradientBrush.GradientStops>> <</LinearGradientBrush>> <</Border.Background>> <<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"/>> <</Border>> <</ControlTemplate>> <</StackPanel.Resources>> <<Button Template="{StaticResource ButtonTemplate}" Padding="10" Margin="10">>
My Button
<</Button>> <</StackPanel>>
[[File:1449_wpf_template03.jpg]]
*例えばボタンの色を変更するためだけにテンプレートを定義する必要があるとしたら、シンプルなプログラミングモデルとは言えない。
*理想的なのはテンプレートにパラメータを追加できるか、テンプレートコントロールのプロパティをお使用して、テンプレートをカスタマイズできること。
<<blockquote>>以下の例は、BorderのBorderThicknessプロパティ、BorderBrushプロパティ、およびBackgroundプロパティをテンプレートが適用されるButtonの同じプロパティにバインドします。Buttonのプロパティを設定するだけで、下図のようなボタンを作成出来ます。<</blockquote>> <<StackPanel Name="MainPanel" Initialized="MainPanel_Initialized" >> <<StackPanel.Resources>> <<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">> <<Border CornerRadius="6"
BorderThickness="{TemplateBinding Property=BorderThickness}"
BorderBrush="{TemplateBinding Property=BorderBrush}"
Background="{TemplateBinding Property=Background}">> <<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"/>> <</Border>> <</ControlTemplate>> <</StackPanel.Resources>> <<Button Template="{StaticResource ButtonTemplate}"
Padding="10"
Margin="10"
BorderThickness="6"
BorderBrush="Blue">>
My Button
<</Button>> <</StackPanel>>
[[File:1450_wpf_template04.jpg]]