WPF 如何在后台向DataGrid中添加一列ComboBox列
不在前台添加,在后台使用XamlReader.Load()方法添加,绑定ComboBox的ItemSource的时候出错。代码如下
private DataGridTemplateColumn CreateComboBoxTemplate(string headername, string bindingname, TitleList t)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = headername;
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns=\"http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation\" ");
CellTemp.Append("xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" ");
CellTemp.Append("xmlns:local=\"clr-namespace:DataGrid_Part1");
CellTemp.Append(";assembly=DataGrid_Part1\">");
CellTemp.Append("<ComboBox ItemsSource=\"{" + t + "}\" SelectedItem=\"{Binding " + bindingname + "}\"></ComboBox>");
CellTemp.Append("</DataTemplate>");
StringReader strreader = new StringReader(CellTemp.ToString());
XmlTextReader xmlreader = new XmlTextReader(strreader);
templateColumn.CellTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Load(xmlreader);
return templateColumn;
}
其中TitleList为一个Dictionary。 --------------------编程问答--------------------
补充:.NET技术 , C#