2013-09-30 38 views
1

指望我有一个模型,例如:增加新的项目,从型号

  • 公司
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)
      • 人(姓名,国籍,新)

此人的 “新” 属性是一个布尔值。我如何在公司的用户界面上对每个部门的新员工数量进行动态统计?

我在我的MainView的XAML中为每个公司设置了一个TextBox,我希望该文本框告诉我公司新人(来自所有部门)的总数。我怎样才能做到这一点?

回答

0

最好的办法是有一个专门的部门视图和视图模型。然后,部门视图模型将显示所有Person实例以及反映所需值的NewPersonCount属性。

下面是新视图模型(省略INotifyPropertyChanged和全部)的粗例如:

public class DepartmentViewModel 
{ 
    public ObservableCollection<Person> People {get; set; } 
    public int NewPeopleCount 
    { 
    get 
    { 
     return People.Where(p => p.New).Count(); 
    } 
    } 
} 

甲部门视图将绑定到它(例如,NewPeopleCountTextBox被示出)。您的主视图很可能会有一个ListView或其他一些ItemsControl绑定到所有部门,显示部门视图。

+0

可不可以给上藿实现它更多的一些细节性? – touyets

+0

调整我的答案与更多细节。 – meilke

+0

它完美的工作,谢谢你 – touyets

0

您可以创建所需的数据结构,并创建一个将返回所需的值

public class Person 
{ 
    public string Name; 
    public string Nationality; 
    public bool New; 
} 

public class Department 
{ 
    public List<Person> EmployeeList; 
    public void Add(Person person) 
    { 
     if (EmployeeList == null) 
      EmployeeList = new List<Person>(); 
     EmployeeList.Add(person); 

    } 
    public int GetNewPersonCount 
    { 
     get 
     { 
      int count = 0; 
      if (EmployeeList != null) 
      { 
       foreach (Person p in EmployeeList) 
       { 
        if (p.New) 
         count++; 
       } 
      } 

      return count; 
     } 
    } 
} 

public class Company 
{ 
    public List<Department> DepartmentList; 
    public void Add(Department department) 
    { 
     if (DepartmentList == null) 
      DepartmentList = new List<Department>(); 
     DepartmentList.Add(department); 


    } 
    public int GetNewPersonCount 
    { 
     get 
     { 
      int count = 0; 
      if (DepartmentList != null) 
      { 
       foreach (Department d in DepartmentList) 
       { 
        count += d.GetNewPersonCount; 
       } 
      } 

      return count; 
     } 
    } 
}