C# 代码
public partial class MainWindow : Window { List<Student> students; public MainWindow() { InitializeComponent(); students = new List<Student>(); students.Add(new Student() { Id = 1, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name1" }, new select() { selectId = 2, TeamName = "Name2" } } }); students.Add(new Student() { Id = 2, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name3" }, new select() { selectId = 2, TeamName = "Name4" } } }); students.Add(new Student() { Id = 3, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name5" }, new select() { selectId = 2, TeamName = "Name6" } } }); dg1.ItemsSource = students; } public class Student { public int Id { get; set; } public int SelectedOption { get; set; } public List<select> options { get; set; } public Student() { options = new List<select>(); } } public class Option { public List<string> myoption { get; set; } public Option() { myoption = new List<string>(); } } public class select { public int selectId { get; set; } public string TeamName { get; set; } } }
xaml:
<DataGrid Name="dg1" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Id}" /> <DataGridComboBoxColumn Header="Options" SelectedValueBinding="{Binding SelectedOption}" SelectedValuePath="selectId" DisplayMemberPath="TeamName"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=options}" /> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=options}" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> </Grid>
SelectedOption属性是默认Combobox选择第几行。