1

私は初心者のWPFです。仕事で新しいプロジェクトを開発していますが、複数選択したファイルエクスプローラーコントロールを挿入する必要があります。

概念は、アクロニスファイルエクスプローラーに類似している必要があります:(チェックボックス付きのツリービュー)

例を参照してください

左のコンテナを見てください。これに似たものを実装する必要があります。グーグルでたくさん検索しましたが、たくさんの実装を見ましたが、これに似たものはありませんでした。

私はWPFの経験があまりないので、始めるのは非常に困難です。

私がそれを行うのに役立つかもしれないいくつかのヒントや同様のプロジェクトがありますか?

私のプロジェクトはMVVMDPに基づいています。

ありがとう

4

2 に答える 2

5

ツリービューの改造は非常に簡単です。バインドしたいコレクションから始めます。

<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;
   }

それともよく似たもの...

于 2012-09-27T15:41:04.080 に答える