我有一个数据网格绑定到一个名为“ ListOfEmployeeDetails”的可观察集合。数据网格具有三列,即“名称”,“出生日期”和“体验”。在第三列的每个单元格(即“经验”)上,我放置了一个按钮,该按钮应显示该员工在其工作期间从事的项目的列表。
在数据网格行上单击按钮时,我无法填充项目列表。
这是我尝试过的:
EmployeeDetails.Xaml
<Datagrid ItemsSource="{Binding ListOfEmployeeDetails}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name"/>
<DataGridTemplateColumn Header="Date Of Birth"/>
<DataGridTemplateColumn Header="Experience">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
x:Name = "DropDownButton"
Tag="{Binding RelativeSource= {RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Path=DataContext}"
Command="{Binding Path=DataContext.LoadDropDownMenu,RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type DataGrid}}}"
CommandParameter="{Binding EmpId }">
<Button.ContextMenu>
//TODO: Need to display the list of Project Names for selected Employee ID
</Button.ContextMenu>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</Datagrid>
EmployeeDetailsViewModel.cs
public class EmployeeDetailsModel
{
public string EmpId { get; set; }
public string EmpName { get; set; }
public string EmpDOB { get; set; }
public string EmpExperience { get; set; }
public ObservableCollection<ProjectModel> ListOfProjects { get; set; }
}
private ObservableCollection<EmployeeDetailsModel> _ListOfEmployeeDetails;
public ObservableCollection<EmployeeDetailsModel> ListOfEmployeeDetails
{
get { return _ListOfEmployeeDetails; }
set { SetProperty(ref _ListOfEmployeeDetails, value); }
}
public ICommand LoadDropDownMenu
{
get { return new DelegateCommand(LoadProjectDetails); }
}
private void LoadProjectDetails(object obj)
{
ListOfProjects.Clear();
foreach(var employeeDetail in ListOfEmployeeDetails)
{
if(employeeDetail.EmpId == obj.ToString())
{
ListOfProjects.Add(role);
}
}
}
public class ProjectModel
{
public string ProjectName { get; set; }
public string ProjectID { get; set; }
}
在上下文中填充项目名称列表应该做些什么更改 菜单。任何帮助将不胜感激。