次の2つのクラスがあります。
public class DeviceGroup
{
public String Name { get; set; }
public ObservableCollection<DeviceGroup> DeviceGroups { get; set; }
public ObservableCollection<Device> Devices { get; set; }
public DeviceGroup()
{
Name = String.Empty;
DeviceGroups = new ObservableCollection<DeviceGroup>();
Devices = new ObservableCollection<Device>();
}
}
public class Device
{
public String Name { get; set; }
}
私のメイン クラスには ObservableCollection があります。
私の Xaml では、次のように、HierachicalDataTemplate 内で DeviceGroup を指定するだけで、ツリービューを簡単に作成できます。
<Window.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type local:Device}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:DeviceGroup}" ItemsSource="{Binding DeviceGroups}">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TreeView ItemsSource="{Binding DeviceGroups}"/>
</Grid>
問題は、DeviceGroup と同様に Devices コレクションを選択するにはどうすればよいかということです。デバイスが Windows エクスプローラー (ディレクトリとファイル) のように表示されるようにしたいと思います。この問題に対する Xaml ソリューションはありますか? または、分離コードで TreeViewItems を作成する必要がありますか。ありがとう。