【虚心求教】DataGrid模板列中comboBOX绑定数据的问题
有一张学生信息表,里面有一个性别字段,我存的是0、1 然后这张表的所有信息绑定在一个DataGrid里面,其中性别这个字段需要弄成模板列 里面要放comboBOX做成下拉框可以选择性别的那种,便于修改。可是到combox绑定那里的时候数据显示不出来…… 代码如下:
xaml:---------------------------
<sdk:DataGrid AutoGenerateColumns="False" Height="340" HorizontalAlignment="Left" Margin="12,107,0,0" Name="stuGrid" VerticalAlignment="Top" Width="500" LoadingRow="stuGrid_LoadingRow">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="选 择">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="checkID"></CheckBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="学生编号" Binding="{Binding ID}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="姓 名" Binding="{Binding StuName}"></sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn Header="姓 别">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Width="80" DisplayMemberPath="Male" Loaded="comMale_Loaded" Name="comMale"></ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="身份证号" Binding="{Binding IDnumber}"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
后台代码:-------------------------------
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Load);
}
//SolidColorBrush brush = new SolidColorBrush(Colors.Red);
StudentDomainContext stuContext = new StudentDomainContext();
private LoadOperation<Student> loadStu;
private List<Student> list { get; set; }
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="obj"></param>
/// <param name="args"></param>
public void MainPage_Load(Object obj,RoutedEventArgs args)
{
loadStu = this.stuContext.Load(this.stuContext.GetStudentQuery());
loadStu.Completed += new EventHandler(loadStu_Completed);
}
/// <summary>
/// 加载完成
/// </summary>
/// <param name="obj"></param>
/// <param name="args"></param>
private void loadStu_Completed(Object obj, EventArgs args)
{
stuGrid.ItemsSource = loadStu.Entities;
}
/// <summary>
/// ComboBOX绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void stuGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (stuContext.HasChanges)
{
stuContext.SubmitChanges();
}
}
private void comMale_Loaded(object sender, RoutedEventArgs e)
{
}
}
是不是DataGrid里面添加了模板咧后,绑定的时候找不到模板咧里面的comboBOX? 需不需要再stuGrid.ItemsSource = loadStu.Entities; 这个集合里面抽出所有的性别 然后添加到新的集合里面 然后再给combox绑定? 求指点……
[解决办法]
这里有几个开源实例可以参考:
http://silverlightchina.net/html/tips/2011/0308/5908.html
http://silverlightchina.net/html/tips/2011/0303/5767.html
http://silverlightchina.net/html/tips/2010/0102/535.html
http://silverlightchina.net/html/developer/silverlight/2009/0927/196.html