我在datagrid中创建了作为datatemplate的列表框,并且该列表框中包含复选框。
private ObservableCollection<EmployeeViewModel> _employees;
public ObservableCollection<EmployeeViewModel> Employees
{
get { return _employees; }
set
{
_employees = value;
this.OnPropertyChanged("Employees");
}
}
public int NumberOfSkills
{
get
{
int count = 0;
foreach(SkillViewModel skill in ((EmployeeViewModel)SelectedRow).Skills)
{
if(skill.IsChecked)
{
count++;
}
}
return count;
}
}
在viewmodel类中,我有Employees集合,其中包含Skills属性(上面的列表框是Skills上的bindend)。同样在viewmodel中我有NumberOfSkills属性beacuse我想在列表框中的所有复选框被检查datagrid行开始闪烁。
public ObservableCollection<SkillViewModel> Skills
{
get
{
ObservableCollection<SkillViewModel> skills = new ObservableCollection<SkillViewModel>();
if (employee.Skills != null)
{
foreach (Skill skill in employee.Skills)
{
skills.Add(new SkillViewModel { Name = skill.Name, IsChecked = skill.IsChecked });
}
}
if (skills.Count == 0)
{
foreach (SkillsEnum skill in Enum.GetValues(typeof(SkillsEnum)))
{
skills.Add(new SkillViewModel { Name = skill.ToString(), IsChecked = false });
}
}
return skills;
}
set
{
List<Skill> list = new List<Skill>();
foreach (SkillViewModel skill in (ObservableCollection<SkillViewModel>)value)
{
list.Add(new Skill { Name = skill.Name, IsChecked = skill.IsChecked });
}
employee.Skills = list;
}
}
技能是EmployeeViewModel类中的属性。
public class SkillViewModel : BaseViewModel
{
public string Name { get; set; }
private bool _isChecked;
public bool IsChecked
{
get
{
return _isChecked;
}
set
{
_isChecked = value;
OnPropertyChanged("NumberOfSkills");
OnPropertyChanged("Skills");
OnPropertyChanged("IsChecked");
}
}
}
SkillViewModel看起来像:
<DataTrigger Binding="{Binding Path=NumberOfSkills}" Value="3">
技能类是可序列化的,因此类无法实现BaseViewModel(INotifyPropertyChanged)。
现在好了...当我在视图中选中或取消选中复选框时,该属性在SkillViewModel中更改。但是我如何通知NumberOfSkills也被更改,因为没有这个数据网格行无法启动闪烁。 DataTrigger看起来:
text
我是WPF的新手,真的尝试这样做。我也知道代码是最好的,你可以帮助我。