不在前台添加,在后台使用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。
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。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货