0
我需要填写我的组合框与我的部门清单,并设置组合框的所选项目和我的员工处WPF组合框的数据绑定填写选择项
型号系
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
}
模范员工:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Department Department { get; set; }
}
视图模型
public class ViewModel
{
public Employee Employee { get; set; }
public ObservableCollection<Department> Departments{ get; set; }
public ViewModel()
{
Employee new Employee()
{
FirstName = "Harry",
LastName = "Park",
Department = new Department { Id = 1, Name = "Department 2" },
};
Departments= new ObservableCollection<Department>()
{
new Department{ Id=1, Name="Department 1" },
new Department{ Id=2, Name="Department 2" },
new Department{ Id=2, Name="Department 3" },
};
}
XAML
<Grid DataContext="{StaticResource ViewModel
<ComboBox
ItemsSource="{Binding Department
SelectedValue="{Binding Employee.Department
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
你能帮助我吗?
看看这里:https://www.codeproject.com/Articles/301678/Step-by-Step-WPF-Data-结合与 - 组合框 – Alander