C#/ WPF中的TreeView数据绑定

时间:2012-11-20 09:12:09

标签: c# wpf xaml treeview

有人可以帮助我理解为TreeView组件和模型创建xaml代码的方法:

class Task: ObservableObject
{
    private string _title;

    public string Title {
        get { return _title; }
        set { 
            if (value != _title) {
                _title = value;
                OnPropertyChanged("Title");
            } 
        }
    }

    public override string ToString() {
        return Title;
    }
}

class Tasks:ObservableCollection<Task>
{

}

class Group:ObservableObject
{
    private Tasks _tasksList;

    public Group() {
        _tasksList = new Tasks();
    }

    public Tasks TasksList {
        get{
            return _tasksList;
        }

        set {
            if (value != _tasksList) {
                _tasksList = value;
                OnPropertyChanged("TasksList");
            }
        }
    }
}

class Groups:ObservableCollection<Group>
{

}

我想拥有的是View with TreeView组件和数据,如

Group1
   Task 1
   Task 2
Group2
   Task 3
   Task 4

使用InputSource和DataContext ...

1 个答案:

答案 0 :(得分:2)

您必须使用hierarhical data templates