2015-04-01 15 views
0
namespace colourchanges 
{ 
    public class Group 
    { 
     public string Name { get; set; } 
//its a class for adding parent list using group class 
    } 

    public class EmployeeTree : INotifyPropertyChanged 
    { 
     public EmployeeTree() 
     { 
      this.GroupStaff = new List<Group>(); 
      GroupStaff.Add(new Group { Name = "Designers" }); 
      GroupStaff.Add(new Group { Name = "Developers" }); 
      GroupStaff.Add(new Group { Name = "Managers" }); 

//here we are declaring list for adding parent list 
     } 

     private List<Group> _GroupStaff; 
     public List<Group> GroupStaff 
     { 
      get { return _GroupStaff; } 
      set 
      { 
       _GroupStaff = value; 
       RaisePropertyChanged("GroupStaff"); 
      } 
     } 
//creates a list for parentlist 

     private Group _selectedGroupStaff; 
     public Group selectedGroupStaff 
     { 
      get { return _selectedGroupStaff; } 
      set 
      { 
       _selectedGroupStaff = value; 
       if (selectedGroupStaff.Name == "Designers") 
       { 
        City = "Chennai"; 
        Country = "India"; 
        Email = "[email protected]"; 
        MobileNo = 9094117917; 
        Address = "Annanagar"; 
       } 
       else if (selectedGroupStaff.Name == "Developers") 
       { 
        City = "Trichy"; 
        Country = "India"; 
        Email = "[email protected]"; 
        MobileNo = 9094667878; 
        Address = "Koyambedu"; 
       } 
       else if (selectedGroupStaff.Name == "Managers") 
       { 
        City = "Salem"; 
        Country = "India"; 
        Email = "[email protected]"; 
        MobileNo = 9094154678; 
        Address = "Arumbakkam"; 
       } 
       RaisePropertyChanged("selectedGroupStaff"); 
      } 
     }//for selecting parent list in order to bind to textbox 


     private string _City; 
     private string _Country; 
     private string _Email; 
     private long _MobileNo; 
     private string _Address; 
//properties of parent list to bind to textbox 
     public string City 
     { 
      get { return _City; } 
      set 
      { 
       _City = value; 
       RaisePropertyChanged("City"); 
      } 
     } 
     public string Country 
     { 
      get { return _Country; } 
      set 
      { 
       _Country = value; 
       RaisePropertyChanged("Country"); 
      } 
     } 
     public string Email 
     { 
      get { return _Email; } 
      set 
      { 
       _Email = value; 
       RaisePropertyChanged("Email"); 
      } 
     } 
     public long MobileNo 
     { 
      get { return _MobileNo; } 
      set 
      { 
       _MobileNo = value; 
       RaisePropertyChanged("MobileNo"); 
      } 
     } 
     public string Address 
     { 
      get { return _Address; } 
      set 
      { 
       _Address = value; 
       RaisePropertyChanged("Address"); 
      } 
     } 
///raise property changed event handler code 


     public event PropertyChangedEventHandler PropertyChanged; 
     public void RaisePropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 



//how to add sub list for designers developers and managers in the constructor 
+0

你可能需要在模型类类型的模型集合属性。你可以将这些项目添加到该属性。 – Karuppasamy 2015-04-01 06:41:28

+0

didn得到你可以你解释清楚我 – Prasanna 2015-04-01 06:52:40

+0

我已经添加了答案,请检查,你可以得到一个主意。您在此使用的Model类是“Group”。因此,您需要一个集合属性,如前所述:Group类中的ObservableColletion ,正如我所添加的。您可以将子列表添加到特定项目的该属性。在我给出的答案中,让管理者为根,而开发者为其子列表。希望你明白 – Karuppasamy 2015-04-01 06:59:57

回答

0

让下面是模型类

public class Group 
{ 
private string _City; 
private string _Country; 
private string _Email; 
private long _MobileNo; 
private string _Address; 
public Group() 
{ 
    Items = new ObservableCollection<Group>(); 
} 
public ObservableCollection<Group> Items { get; set; } 
} 

,并在视图模型的构造函数,你可以添加的项目。

public EmployeeTree() 
{ 
    this.GroupStaff = new List<Group>(); 
    Group rootGroup = new Group(){Name ="Manager"}; 
    Group childGroup = new Group(){Name = "Developer"}; 
    rootGroup.Items.Add(childGroup); 
    this.GroupStaff.Add(rootGroup); 
    } 

这是用于分层结构。希望你正在寻找这个。

而且你的XAML应该是这样的

<TreeView Name="GroupTreeView"> 
<TreeView.ItemTemplate> 
    <HierarchicalDataTemplate ItemsSource="{Binding Items}"> 
    <TextBlock Text="{Binding Name}" /> 
    </HierarchicalDataTemplate> 
</TreeView.ItemTemplate> 

+0

评论不适合长时间讨论;这个谈话已经[转移到聊天](http://chat.stackoverflow.com/rooms/74269/discussion-on-answer-by-karuppasamy-how-to-add-sublist-in-wpf-using-mvvm) 。 – Taryn 2015-04-01 14:48:21

相关问题