WPFのaControlTemplate
とaの違いは何ですか?DataTemplate
7 に答える
通常、コントロールはそれ自体のためにレンダリングされ、基になるデータを反映しません。たとえば、 aButton
はビジネス オブジェクトにバインドされません。純粋にそこにあるため、クリックできます。ContentControl
ただし、またはは通常ListBox
、ユーザーにデータを提示できるように表示されます。
したがってDataTemplate
、 は基になるデータの視覚的な構造を提供するために使用されますが、 は基にControlTemplate
なるデータとは関係なく、単にコントロール自体に視覚的なレイアウトを提供します。
通常、 には式ControlTemplate
のみが含まTemplateBinding
れ、コントロール自体のプロパティにバインドされますが、DataTemplate
には標準のバインディング式が含まれ、そのプロパティDataContext
(ビジネス/ドメイン オブジェクトまたはビュー モデル) にバインドされます。
非常に基本的にはControlTemplate
、コントロールを表示する方法をDataTemplate
説明し、データを表示する方法を説明します。
例えば:
ALabel
はコントロールであり、あるコンテンツ (または別のコントロール)の周りにa を使用して表示ControlTemplate
する必要があることを示す a が含まれます。Label
Border
DataTemplate
Customer
クラスは Data であり、 を使用して表示されます。これは、名前を表示する 1 つと電話番号を表示する2 つを含むとしてタイプDataTemplate
を表示すると言うことができます。すべてのクラスが を使用して表示されることに注意してください。通常は、オブジェクトのメソッドの結果にプロパティが設定されたデフォルト テンプレートを使用します。Customer
StackPanel
TextBlocks
DataTemplates
TextBlock
Text
ToString
Troels LarsenがMSDN フォーラムで適切な説明をしています
<Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <DataTemplate x:Key="ButtonContentTemplate"> <StackPanel Orientation="Horizontal"> <Grid Height="8" Width="8"> <Path HorizontalAlignment="Stretch" Margin="0,0,1.8,1.8" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" Data="M0.5,5.7 L0.5,0.5 L5.7,0.5"/> <Path HorizontalAlignment="Stretch" Margin="2,3,0,0" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" Data="M3.2,7.5 L7.5,7.5 L7.5,3.5"/> <Path HorizontalAlignment="Stretch" Margin="1.2,1.4,0.7,0.7" VerticalAlignment="Stretch" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M2.5,2.5 L7.5,7.5"/> <Path HorizontalAlignment="Stretch" Margin="1.7,2.0,1,1" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" Data="M3,7.5 L7.5,7.5 L7.5,3.5"/> <Path HorizontalAlignment="Stretch" Margin="1,1,1,1" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" Data="M1.5,6.5 L1.5,1 L6.5,1.5"/> </Grid> <ContentPresenter Content="{Binding}"/> </StackPanel> </DataTemplate> <ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate"> <Grid> <Ellipse Fill="{TemplateBinding Background}"/> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Window.Resources> <StackPanel> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="1"/> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="2"/> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="3"/> </StackPanel> </Window>
(テンプレートは、 http : //msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate.aspx および http://msdn.microsoft.com/en-us/library/system.windowsから露骨に盗まれました.controls.contentcontrol.contenttemplate%28VS.95%29.aspx それぞれ)
とにかく、ControlTemplate は Button 自体の外観を決定し、ContentTemplate はボタンの Content の外観を決定します。そのため、コンテンツをデータ クラスの 1 つにバインドし、必要に応じて表示することができます。
ControlTemplate
: コントロール スタイルを表します。
DataTemplate
: データのスタイル (データをどのように表示したいか) を表します。
すべてのコントロールは、テンプレート プロパティを介してオーバーライドできる既定のコントロール テンプレートを使用しています。
たとえば、
Button
テンプレートはコントロール テンプレートです。
Button
コンテンツ テンプレートはデータ テンプレートです
<Button VerticalAlignment="Top" >
<Button.Template>
<ControlTemplate >
<Grid>
<Rectangle Fill="Blue" RadiusX="20" RadiusY="20"/>
<Ellipse Fill="Red" />
<ContentPresenter Content="{Binding}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="50">
<TextBlock Text="Name" Margin="5"/>
<TextBox Text="{Binding UserName, Mode=TwoWay}" Margin="5" Width="100"/>
<Button Content="Show Name" Click="OnClickShowName" />
</StackPanel>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
public String UserName
{
get { return userName; }
set
{
userName = value;
this.NotifyPropertyChanged("UserName");
}
}
ControlTemplate
- 要素の外観を変更します。たとえばButton
、画像とテキストを含めることができます
DataTemplate
- 要素を使用して基になるデータを表す。
ControlTemplate
視覚的な外観を定義DataTemplate
し、データ項目の視覚的な外観を置き換えます。
例: ボタンを長方形から円形に表示したい => コントロール テンプレート。
また、コントロールに複雑なオブジェクトがある場合はToString()
、 を呼び出して表示するだけで、DataTemplate
さまざまなメンバーを取得して、データ オブジェクトの値を表示および変更できます。