私はいくつかのノードを表示する必要があるWPFツリーを持っています。 EntityAとEntityBの2種類のエンティティがあるとします。これらの両方のエンティティは、共通のインタフェースIEntityを実装します。今、EntityAは、EntityB要素とEntityA要素のコレクションを持ちます。これをHierarchicalDataTemplateでどのように表示できますか?WPFツリーの階層データテンプレートに関する質問
私はEntityA型の要素を含む私のVM内の "DisplayItems"と呼ばれるObservableCollection()を公開しています。
EnittyAとEntityBの両方に、 "ItemCollection"というObservableCollectionが追加されます。 EntityAの場合、ItemCollectionリストには理想的にはEntityAおよびEntityB型のエンティティが含まれている必要があります。
次のように現在のHierarchicalDataTemplateと私が使用していますXAMLは次のとおりです。
<HierarchicalDataTemplate ItemsSource="{Binding Path=ItemCollection}" DataType="{x:Type Entities:EntityB}">
<Grid>
<StackPanel Orientation="Horizontal" x:Name="compositeCT">
<Image Source="/Images/EntityB.png" Width="15" Height="15"/>
<Label Foreground="Blue" Content="{Binding Path=Name}"/>
<Label Foreground="Black" Content=" = "/>
<Label Foreground="Blue" Content="{Binding Path=CompositeLabel}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" x:Name="nCompositeCT">
<Image Source="/Images/EntityB.png" Width="15" Height="15"/>
<TextBlock Foreground="Blue" Text="{Binding Path=Name}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="Green" Text="{Binding}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=ItemCollection}" DataType="{x:Type Entities:EntityA}">
<StackPanel Orientation="Horizontal" >
<Image Source="/Images/ElementA.png" Margin="3" Width="15" Height="15" Focusable="False"/>
<TextBlock Foreground="Red" Text="{Binding Path = Name}" Focusable="False"/>
</StackPanel>
</HierarchicalDataTemplate>
<TreeView x:Name="tvMyTree"
ItemsSource="{Binding DisplayItems}"
AllowDrop="True"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True"
Margin="5"
TreeViewItem.Expanded="OnTreeViewItemExpanded"
TreeViewItem.Selected="OnTreeViewItemSelected"
/>
が見えます –