ツリービューの改造は非常に簡単です。バインドしたいコレクションから始めます。
<Grid>
<TreeView ItemsSource="{Binding Folders}"/>
</Grid>
ただし、バインドしたデータを表示する方法を定義する必要があります。あなたのアイテムは、FolderViewModels と FileViewModels (どちらも Name プロパティを持っています) の単なる IEnumerable (任意のリストまたは配列) であると想定しているので、これらを表示する方法を説明する必要があります。DataTemplate を定義することでこれを行います。これはツリー用であるため、subItems も定義する HeirarchicalDataTemplate を使用します。
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewModel:FolderViewModel}"
ItemsSource="{Binding SubFoldersAndFiles}">
<CheckBox Content="{Binding Name}"/>
</HierarchicalDataTemplate>
<Grid.Resources/>
ファイルは同じですが、サブアイテムは必要ありません
<HierarchicalDataTemplate DataType="{x:Type viewModel:FolderViewModel}">
<CheckBox Content="{Binding Name}"/>
</HierarchicalDataTemplate>
すべてをまとめると、
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewModel:FolderViewModel}"
ItemsSource="{Binding SubFoldersAndFiles}">
<CheckBox Content="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type viewModel:FolderViewModel}">
<CheckBox Content="{Binding Name}"/>
</HierarchicalDataTemplate>
<Grid.Resources/>
<TreeView ItemsSource="{Binding Folders}"/>
</Grid>
アイコン アイコン
を表示したい場合は、CheckBox の内容を変更します。ViewModel で Image を定義すると仮定しています。
<CheckBox>
<CheckBox.Content>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</CheckBox.Content>
選択
最後に、アイテムの選択を処理する必要があります。FileViewModel と FolderViewModels に IsSelected プロパティを追加することをお勧めします。ファイルの場合、これは信じられないほど単純で、単なるブール値です。
public class FileViewModel : INotifyProperty
...
public bool IsSelected //Something here to handle get/set and NotifyPropertyChanged that depends on your MVVM framework, I use ReactiveUI a lot so that's this syntax
{
get { return _IsSelected;}
set { this.RaiseAndSetIfChanged(x=>x.IsSelected, value); }
}
と
<CheckBox IsChecked="{Binding IsSelected}">
FolderViewModel の場合はもう少し複雑です。そのロジックについては、すぐに説明します。まず Xaml で、現在の CheckBox 宣言を次のように置き換えます。
<CheckBox IsThreeState="True" IsChecked="{Binding IsSelected}">
<!--IsChecked = True, False or null-->
Nullable<bool>
したがって、 (aka )のセットを返す必要がありbool?
ます。
public bool? IsSelected
{
get
{
if (SubFoldersAndFiles.All(x=>x.IsSelected) return true;
if (SubFoldersAndFiles.All(x=>x.IsSelected==false) return false;
return null;
}
set
{
// We can't set to indeterminate at folder level so we have to set to
// set to oposite of what we have now
if(value == null)
value = !IsSelected;
foreach(var x in SubFoldersAndFiles)
x.IsSelected = value;
}
それともよく似たもの...