<Grid>
<DataGrid Name="datagrid1" Margin="148,68,0,0" AutoGenerateColumns="False" Height="131" Width="244">
<DataGrid.Columns>
<DataGridTextColumn Header="科室" Width="105" Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="人数" Binding="{Binding Path=Number}" />
<DataGridComboBoxColumn ItemsSource="{Binding Path=People}" SelectedItemBinding="{Binding Path=K_科长}"
Header="科长" />
</DataGrid.Columns>
</DataGrid>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<Company> obc = new ObservableCollection<Company>();
obc.Add(new Company() { Name = "人教科",Number=2,K_科长="熊俊",People=new List<string>(){"熊俊","王晓"}});
obc.Add(new Company() { Name = "广告科", Number = 2, K_科长 = "汤姆", People = new List<string>() { "汤姆", "杰克" } });
obc.Add(new Company() { Name = "财会科", Number = 2, K_科长 = "李莹", People = new List<string>() { "李莹", "张三" } });
obc.Add(new Company() { Name = "人教科", Number = 4, K_科长 = "李刚", People = new List<string>() { "李刚", "王二","牛熊","刘麻子" } });
datagrid1.ItemsSource = obc;
}
}
class Company
{
public string Name { get; set; }
public int Number { get; set; }
public string K_科长 { get; set; }
public List<string> People { get; set; }
}
class People
{
pubic string Name{set;get;}
public override bool Equals(object obj)
{
if (this == obj) return true;
if (obj == null || this.GetType() != obj.GetType())
{
return false;
}
People p = obj as People;
if(p=null) return false;
return this.Name == obj.Name;
}
}
<DataGridTemplateColumn Header="科长">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding People}" SelectedItem="{Binding Path=K_科长}"></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
为了弥补我看错了楼主的问题,经过搜索找到了解决楼主的问题方法。
使用模板列可以满足楼主的需求
<DataGridTemplateColumn Header="科长">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding People}" SelectedItem="{Binding Path=K_科长}"></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。