&tag(WPF/DataTemplate); *目次 [#nd026688] #contents *関連ページ [#qdf91b7b] *参考情報 [#v6521417] *ControlTemplateとDataTemplateの違いって [#r423a38f] -[[Difference between Control Template and DataTemplate in WPF - Stack Overflow:http://stackoverflow.com/questions/1340108/difference-between-control-template-and-datatemplate-in-wpf]]によると、 --DataTemplateは内部データのためのビジュアルレイアウトを提供し、ControlTemplateは内部データには慣用しないビジュアルレイアウトを提供する。 --それゆえ、ControlTemplateはTemplateBindingだけを使う。DataTemplateは通常のバインディングを使う(ビジネス・ドメインオブジェクトと連携)といったことなのか。 --カスタマイズされた汎用部品を作るときは、ControlTemplateを使い、何かのきっかけで見た目をかえたりするソフトごとの専用処理を行う場合DataTemplateということでいいのかな。 *Tips [#ce6ea53e] **リストボックスの選択値に応じてペインを書き換える。 [#c4202508] -[[xaml - WPF: How to set the data template trigger for content control? - Stack Overflow:http://stackoverflow.com/questions/5771362/wpf-how-to-set-the-data-template-trigger-for-content-control]]が参考になる。 -リストで"1"を選択したときは、"template1"が、"2"を選択したときは"template2"が使われるようなxaml #pre{{ <Window x:Class="ControlDemo.OtherDemo.ComboRelatedPane.ComboRelatedPaneWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ComboRelatedPaneWindow" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.Resources> <DataTemplate x:Key="template1"> <TextBlock>template1</TextBlock></DataTemplate> <DataTemplate x:Key="template2"> <TextBlock>template2</TextBlock> </DataTemplate> </Grid.Resources> <StackPanel Orientation="Horizontal"> <ComboBox HorizontalAlignment="Left" Width="200" ItemsSource="{Binding Path=PaneTypes}" DisplayMemberPath="Label" SelectedValuePath="Code" SelectedValue ="{Binding PaneTypeCode}" /> </StackPanel> <ContentControl Grid.Row="1" Content="{Binding}"> <ContentControl.Style> <Style TargetType="ContentControl"> <Style.Triggers> <DataTrigger Binding="{Binding PaneTypeCode}" Value="1"> <Setter Property="ContentTemplate" Value="{StaticResource template1}" /> </DataTrigger> <DataTrigger Binding="{Binding PaneTypeCode}" Value="2"> <Setter Property="ContentTemplate" Value="{StaticResource template2}" /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> </Grid> </Window> }} -[[c# - WPF How to access control from DataTemplate - Stack Overflow:http://stackoverflow.com/questions/4586106/wpf-how-to-access-control-from-datatemplate]]のようにコードビハインドで無理矢理する方法も覚えておいてもよいかも。