2010-03-07 100 views
4

有人會如何將複選框添加到XAML中樹視圖的子項?我的目標是擁有一個樹狀視圖,其中父級只是一個文本塊,所有孩子都是複選框,但一次只能檢查一個孩子。我沒有問題,使整個樹視圖複選框,但我不知道如何得到我真正想要的任何建議?WPF TreeView和複選框

謝謝。

回答

5

要做到這一點,最簡單的方法是調整數據的形狀,以便樹視圖可以用您描述的方式表示。下面是對應於您的類型樹的最小數據結構的例子:

public class CheckedList 
{ 
    public string Title { get; set; } 

    public ObservableCollection<CheckedItem> Items { get; private set; } 

    public CheckedList() 
    { 
     Items = new ObservableCollection<CheckedItem>(); 

     //DEBUG: Test data 
     Title = "Test Title"; 
     Items.Add(new CheckedItem("Item 1", true)); 
     Items.Add(new CheckedItem("Item 2", false)); 
    } 
} 

public class CheckedItem : DependencyObject 
{ 
    public static readonly DependencyProperty StateProperty = 
     DependencyProperty.Register("StateProperty", typeof(bool), typeof(CheckedItem), new UIPropertyMetadata(false)); 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("TextProperty", typeof (string), typeof (CheckedItem), new UIPropertyMetadata(string.Empty)); 

    public bool State 
    { 
     get { return (bool)GetValue(StateProperty); } 
     set { SetValue(StateProperty, value); } 
    } 

    public string Text 
    { 
     get { return (string) GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public CheckedItem(string text, bool state) 
    { 
     Text = text; 
     State = state; 
    } 
} 

這裏是XAML和代碼隱藏數據模板的窗口和樹視圖來表示數據與複選框文本標題項目:

<Window x:Class="TestApp.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:TestApp" 
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> 

<Window.Resources> 
    <HierarchicalDataTemplate DataType="{x:Type local:CheckedList}" ItemsSource="{Binding Items}"> 
     <TextBlock Text="{Binding Title}" /> 
    </HierarchicalDataTemplate> 

    <DataTemplate DataType="{x:Type local:CheckedItem}"> 
     <CheckBox Content="{Binding Text}" IsChecked="{Binding State, Mode=TwoWay}"></CheckBox> 
    </DataTemplate> 
</Window.Resources> 

<Grid> 
    <TreeView x:Name="ExampleTree"></TreeView> 
</Grid> 

代碼隱藏:

public partial class Window1 : Window 
{ 
    ObservableCollection<CheckedList> _lists = new ObservableCollection<CheckedList>(); 

    public Window1() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     //DEBUG: Test data 
     _lists.Add(new CheckedList()); 
     _lists.Add(new CheckedList()); 
     ExampleTree.ItemsSource = _lists; 
    } 
} 

ü唱ObservableCollection和DependencyObject允許樹和數據結構保持同步。當用戶點擊樹中的項目時,您應該能夠查看您的列表並查看修改。或者,如果您修改數據,它應該反映在樹中。

+0

我剛剛注意到你一次只想檢查一個項目。如果是這樣,您可以修改CheckedList以偵聽CheckedItem.State的更改並執行您的規則。您可能需要考慮使用單選按鈕而不是複選框,因爲大多數用戶希望複選框允許多選。 –

7

你爲什麼不直接在代碼中做?像這樣:

 TreeViewItem newItem = new TreeViewItem() 
     { 
      Header = "One" 
     }; 

     treeViewObjects.Items.Add(newItem); 

     TreeViewItem newItem1 = new TreeViewItem() 
     { 
      Header = new CheckBox() 
      { 
       Content = "Two" 
      } 
     }; 
     newItem.Items.Add(newItem1); 
+1

簡單而高效。謝謝。 – eVerano

+0

我愛你的男人。 –

+0

嗨,這是工作,但只是爲了子項目。如何添加複選框到父項目? –