2014-01-28 46 views
0

這是我繼承的代碼庫,而System.Windows.Controls命名空間中的TreeView顯然不支持CheckBoxes屬性。我試圖切換到System.Windows.Forms,但是必須更改太多的代碼。是否可以添加複選框到System.Windows.Control.TreeView,如果是這樣,怎麼樣?謝謝。在C#,.Net 3.5中,如何將複選框添加到TreeView?

+0

你在說WPF嗎? – DonBoitnott

+0

是的,我正在談論WPF – scrayne

+1

編輯您的標籤以包含'WPF'。 – DonBoitnott

回答

0

您可以通過影響HierarchicalDataTemplate樹視圖的ItemTemplate屬性。只需定義模板是這樣的:

<HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding YourNodeInnerElementsCollectionHere, Mode=OneTime}"> 
    <StackPanel Orientation="Horizontal"> 
    <CheckBox Focusable="False" IsChecked="{Binding YourBooleanPropertyForCheck}" VerticalAlignment="Center"/> 
    <ContentPresenter Content="{Binding YourStringPropertyForText, Mode=OneTime}"/> 
    </StackPanel> 
</HierarchicalDataTemplate> 

然後設置你的樹狀如下:

<TreeView (...) ItemTemplate="{StaticResource CheckBoxItemTemplate}" (...) /> 

UPDATE

這裏有一個模板樹視圖的一個非常簡單的例子。

項目:WpfApplication4(根名稱空間)

文件1:MyItemCollection.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WpfApplication4 
{ 
    public class MyItemCollection : System.Collections.ObjectModel.ObservableCollection<MyItem> 
    { 

     public MyItemCollection() 
     { 
      Add(new MyItem() { MyText = "test", MyIsChecked = true }); 
      Add(new MyItem() { MyText = "test2", MyIsChecked = false }); 
      Add(new MyItem() { MyText = "test3", MyIsChecked = false }); 
      Add(new MyItem() { MyText = "test4", MyIsChecked = true }); 

      this[0].MyInnerCollection.Add(new MyItem() { MyText = "innertest", MyIsChecked = true }); 
     } 

    } 

    public class MyItem 
    { 

     public MyItem() 
     { 
      _MyInnerCollection = new System.Collections.ObjectModel.ObservableCollection<MyItem>(); 
     } 

     // Fields... 
     private System.Collections.ObjectModel.ObservableCollection<MyItem> _MyInnerCollection; 
     private bool _MyIsChecked; 
     private string _MyText; 

     public string MyText 
     { 
      get { return _MyText; } 
      set 
      { 
       _MyText = value; 
      } 
     } 

     public bool MyIsChecked 
     { 
      get { return _MyIsChecked; } 
      set 
      { 
       _MyIsChecked = value; 
      } 
     } 

     public System.Collections.ObjectModel.ObservableCollection<MyItem> MyInnerCollection 
     { 
      get 
      { 
       return _MyInnerCollection; 
      } 
     } 

    } 
} 

文件2:MainWindow.xaml

<Window x:Class="WpfApplication4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication4" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Resources> 
      <local:MyItemCollection x:Key="ColObj"></local:MyItemCollection> 
      <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding MyInnerCollection}"> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox Focusable="False" IsChecked="{Binding MyIsChecked}" VerticalAlignment="Center"/> 
        <ContentPresenter Content="{Binding MyText}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
     </Grid.Resources> 
     <TreeView ItemTemplate="{StaticResource CheckBoxItemTemplate}" ItemsSource="{StaticResource ColObj}"> 
     </TreeView> 
    </Grid> 
</Window> 

再次,這是爲了示範的目的。 :)它應該給你關於treeview項目模板的主要想法。

+0

謝謝 - 我會試試 – scrayne

+0

不客氣。你可能想玩邊際等,但這應該讓你開始。 :) – Crono

+0

我將你的代碼完全複製到我的源代碼中。當我的樹被加載時,我沒有看到任何複選框,並且在輸出中收到了這些消息: – scrayne

相關問題