ItemsControl
のようなより一般的なオブジェクトではなく、それぞれが異なるオブジェクト (貧乏人ContentPresenter
のグリッドを考えてください) にバインドされていますListBox
。
anまたは anを明示的に指定しListBox
ない場合は、純粋にデータ型に基づいてテンプレートを適用する内部セレクターがあるようです。しかし、私たちの はそれらを拾っていません。また、代わりに に切り替えようとしましたが、それもうまくいきませんでした。ItemTemplate
ItemTemplateSelector
ContentPresenter
ContentControl
今、私はこれを行う独自のものを簡単に書くことができることを知っていますが、DataTypeTemplateSelector
その機能がすでにどこかで「焼き付けられている」かどうか疑問に思っています。論文...ItemsControl
ListBox
TreeView
ComboBox
DataGrid
http://msdn.microsoft.com/en-us/library/ms742521.aspx
...デフォルトで動作するはずです! しかし、繰り返しますが、そうではありません。
これが私たちの(疑似)コードです...
<UserControl.Resources>
<!-- These all work when the relevant items are in a ListBox,
but not with stand-alone ContentPresenters or ContentControls -->
<DataTemplate DataType="local:SomeTypeA">
<TextBlock Text="{Binding Converter={c:DataTypeNameConverter}}" Foreground="Blue" />
</DataTemplate>
<DataTemplate DataType="local::SomeTypeB">
<TextBlock Text="{Binding Converter={c:DataTypeNameConverter}}" Foreground="Purple" />
</DataTemplate>
<DataTemplate DataType="local::SomeTypeC">
<TextBlock Text="{Binding Converter={c:DataTypeNameConverter}}" Foreground="Purple" />
</DataTemplate>
</UserControl.Resources>
<!-- These don't pick up the templates -->
<ContentControl Content="{Binding Field1}" />
<ContentPresenter Content="{Binding Field2}" />
<!-- This however does -->
<ListBox ItemsSource="{Binding AllItems}"
では... 試してみたい人はいませんか?