2011-07-18 12 views
1

ツリービュー(TV)のコンテンツを動的に生成し、ツリーのソースに使用しているアイテムの特定のプロパティに応じて、 TreeViewItems(TVIs)です。1つのTreeViewItemにボタンをプログラムで追加する方法

アプリケーション内の特定のテレビのすべてのTVIにこの機能が必要なだけです。タスクを単純化するものは、そのツリーのすべてのルート項目にボタンが必要であるという事実です。換言すると、特定のTVの中のすべてのTVIは、TVI内にあるTVIを除いてボタンを必要とする。

私はテンプレートの考えを考えていましたが、テレビのすべてのTVIにボタンを追加する方法しか見ることができません。別のオプション(私は...私はまだこれを試していないが、私の頭の中で大丈夫だと思う)は、テレビのすべてにボタンを追加し、ボタンの可視性にソース項目の関連プロパティをバインドすることですこれは完全なハックのように思えますし、私は主により洗練されたソリューションが必要であると確信しています。

この問題を解決する方法についてのご意見はありますか?

+0

あなたは何を試みましたか?あなたはTreeViewの項目をループすることができるはずですが、私はどの項目がルーツで、どれが子供かを判断する方法があると確信しています。 –

答えて

2

ビューモデルのプロパティからデータを動的に作成する場合は、DataTemplateSelectorを使用して調べることができます。

実装では、データ項目に子があるかどうかを確認します。表示されている場合は、ボタンを使用してデータテンプレートを返します。表示されない場合は、ボタンなしでデータテンプレートを返します。ような何か:

public class ButtonDataTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, 
     DependencyObject container) 
    { 
     FrameworkElement element = container as FrameworkElement; 

     if (element != null) 
     { 
      YourClass itemAsYourClass = item as YourClass; 

      if(itemAsYourClass != null) 
      { 
       bool hasChildren = itemAsYourClass.Children != null 
        && itemAsYourClass.Children.Count > 0; 

       return element.FindResource(hasChildren 
        ? "withButtonTemplate" 
        : "withoutButtonTemplate" 
        ); 
      } 
     } 

     return null; 
    } 
} 
+0

ありがとう、私の人生を救った。私はこれを取り除く方法の詳細を見たいと思っている人のために私のコードを以下に載せました – Sheena

0

は、ここで私が使用したコードです:

クラスCurrentDriveDataTemplateSelector:DataTemplateSelector { 公共のDataTemplate currentDriveTemplate {取得します。 set;} パブリックDataTemplate dirTemplate {get;セット; }

public override DataTemplate SelectTemplate(object item, 
     DependencyObject container) 
    { 
     FrameworkElement element = container as FrameworkElement; 

     if (element != null) 
     { 
      Directory dir = item as Directory; 

      if (dir != null) 
      { 
       bool isCurrentDrive = ((dir.parent == 0) && (dir.state == "current")); 

       if (isCurrentDrive) 
       { 
        return currentDriveTemplate; 
       } 
       return dirTemplate; 
      } 
     } 
    } 
} 

<HierarchicalDataTemplate x:Key="normalDirTemplate" ItemsSource="{Binding Path=childdirs}"> 
     <TextBlock Text="{Binding Path=name}"/> 
    </HierarchicalDataTemplate> 

    <HierarchicalDataTemplate x:Key="currentDriveTemplate" ItemsSource="{Binding Path=childdirs}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=name}" Width="100"/> 
      <Button Content="=>" Width="20"></Button> 
     </StackPanel> 
    </HierarchicalDataTemplate> 

    <mine:CurrentDriveDataTemplateSelector 
      currentDriveTemplate="{StaticResource currentDriveTemplate}" 
      dirTemplate="{StaticResource normalDirTemplate}" 
      x:Key="CurrentDriveDataTemplateSelector"/> 

    ...blah 

<TreeView Height="324" Margin="0,25,0,0" x:FieldModifier="private" x:Name="currentDirectoryTree" Width="150" ItemTemplateSelector="{StaticResource CurrentDriveDataTemplateSelector}"> 
    ...etc 
関連する問題