我需要在datagrid中生成一个组合框并绑定所选项。 我使用字典绑定数据网格。值被绑定但不是所选项目。这是我的一段代码!
private DataGridTemplateColumn CreateColumnEditTemplate(string fieldName, ObservableCollection<CommonFieldValue> fieldNames)
{
DataGridTemplateColumn outData = new DataGridTemplateColumn();
StringBuilder xamlData = new StringBuilder();
xamlData.Append("<DataTemplate ");
xamlData.Append("xmlns='http://schemas.microsoft.com/winfx/");
xamlData.Append("2006/xaml/presentation' ");
xamlData.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
xamlData.Append("xmlns:basics='clr-namespace:System.Windows.Controls;");
xamlData.Append("assembly=System.Windows.Controls' >");
xamlData.Append("<ComboBox SelectedItem='{Binding " + fieldName + ",Mode=TwoWay}' SelectedValuePath=\"Content\" >");
foreach (var field in fieldNames)
xamlData.Append("<ComboBoxItem Content='" + field.FieldValue + "'/>");
xamlData.Append("</ComboBox> ");
xamlData.Append("</DataTemplate>");
outData.CellEditingTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Load(xamlData.ToString());
return outData;
}
正在创建模板列并添加datatemplate。