| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

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

案内メニュー