実行時に にバインドされる がありItemsControlます。正常に動作するタイプのデータ テンプレートを定義しました。ItemsSourceObservableCollection<Component>Component
今Componentは があり、すべてのコントロールをレンダリングするために、内部にObservableCollection<Control>別のものを追加したいと考えています。これは、wpf コントロールに関連しない独自のカスタム オブジェクトです。ItemsControlComponent DatatemplateControl
ItemTemplateSelectorコントロールにはさまざまな種類があるため、各タイプに適したテンプレートを選択するために使用しようとしています。以下の例では、小さく保つために、オーバーライドで"RWString"a を使用しているテンプレートの 1 つだけを示しています。ただし、呼び出されることはありません(ブレークポイントを使用して確認します)。私のxamlに何か問題がありますか?FindResourceMyControlTemplateSelectorSelectTemplateSelectTemplate
<ItemsControl.Resources>
<src:MyControlTemplateSelector x:Key="XSelector" />
<DataTemplate DataType="{x:Type src:Component}" >
<Expander Visibility="{Binding Path=Show}">
<ItemsControl ItemsSource="{Binding Path=Contrls}"
ItemTemplateSelector="{StaticResource XSelector}">
<ItemsControl.Resources>
<DataTemplate x:Key="RWstring" >
<TextBlock Text="{Binding Path=Label}"/>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Expander>
</DataTemplate>
</ItemsControl.Resources>
更新: Contrlsはタイプミスではなく、愚かな命名システムを使用しているだけです。Contrlsタイプ の Component のプロパティですObservableCollection<Control>。また、私が使用しようとしている理由は 、ジェネリック型のオブジェクトが含まれているItemsTemplateSelectorなどのすべての派生元であり、明らかにジェネリック型を参照するデータテンプレートを作成できないためです。ObservableCollection<Control>Control<int> Control<string>Control
Update3: Update 2 は関連がなかったため削除しました。にItemTemplateSelector変更することで動作するStaticResourceようになりましたDynamicResource。しかし、なぜこれが機能するのかわかりません...