积极答复者
DataGrid中动态增加ComBox列的问题

问题
-
我遇到的问题是在创建ComBox列时,遇到下面这个问题,最主是方法的传入参数,如何给xaml脚本。
private DataGridColumn GetComboBoxColumn(List<string> itemsoruce) { DataGridTemplateColumn d1 = new DataGridTemplateColumn(); StringBuilder CellTemp = new StringBuilder(); CellTemp.Append("<DataTemplate "); ....省略代码... CellEditTemp.Append("<Grid>"); CellEditTemp.Append("<ComboBox >"); CellEditTemp.Append("</ComboBox ItemSource=这里是方法的传入参数itemsource但我不知道怎么写,谢谢>"); CellEditTemp.Append("</Grid>"); CellEditTemp.Append("</DataTemplate>"); }
答案
-
你好,
建议你用以下方法:
1. 在XAML中定义一个DataTemplate,其中包含ComboBox(定义在控件的Resources中)
2. 在后台代码中获取这个DataTemplate:
DataTemplate dataTemplate = this.Resources["DataTemplate的Key"] as DataTemplate;
3.将步骤2中得到的DataTemplate动态加入给你的DataGrid。
Daoping Liu - MSFT- 已标记为答案 Daoping Liu - MSFTModerator 2011年11月9日 3:19
全部回复
-
你好,
建议你用以下方法:
1. 在XAML中定义一个DataTemplate,其中包含ComboBox(定义在控件的Resources中)
2. 在后台代码中获取这个DataTemplate:
DataTemplate dataTemplate = this.Resources["DataTemplate的Key"] as DataTemplate;
3.将步骤2中得到的DataTemplate动态加入给你的DataGrid。
Daoping Liu - MSFT- 已标记为答案 Daoping Liu - MSFTModerator 2011年11月9日 3:19
-
//本示例只是解决你提出的动态生成数据源问题,并不是一个实用的实例。
//因为,只是解决了下拉数据源的动态设置问题,
//并没有解决ComoboBox和DataGrid对应内容的同步问题。
//如果你有足够的耐心,通过XamlReader可以动态生成任何东西。
//需要注意的是:通过XamlReader创建对象,
//必须给出写C#代码的程序集名称和命名空间,
//这样才能确保XamlReader和你C#代码有机结合。
//微软提供的ComboBox实在做的太烂,居然不支持输入,
//所以TFSoft从来不用这个ComboBox,都是自己定义一个类似的东西代替。
//TFSoft只是计算机程序设计爱好者,不从事IT行业。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;namespace Test
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
if (!DesignerProperties.IsInDesignTool) { DoTest(); }
}//测试一下CreateDataTemplateUsingXamlStr是否达到如期效果
private void DoTest()
{
this.DgrTest.AutoGenerateColumns = false;
this.DgrTest.ItemsSource = (new CSampleDataGridDataSource()).SampleDataGridDataSource;
this.DgrTest.Columns.Clear();
this.DgrTest.Columns.Add(new DataGridTemplateColumn()
{
Header = "Course",
Width = new DataGridLength(100),
CellTemplate = CreateDataTemplateUsingXamlStr(),
});
this.DgrTest.Columns.Add(new DataGridTextColumn()
{
Header = "Score",
Width = new DataGridLength(100),
Binding = new Binding { Path = new PropertyPath("Score") },
});
var X = new ComboBox();
}//完全用Xaml字符串生成带ComboBox的DataTemplate示例
public DataTemplate CreateDataTemplateUsingXamlStr()
{
var Xaml = "";
Xaml += @"<DataTemplate" + "\r\n";
Xaml += @" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""" + "\r\n";
Xaml += @" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""" + "\r\n";
Xaml += @" xmlns:my=""clr-namespace:{AsmName};assembly={NameSpace}"">" + "\r\n";
Xaml += @" <Grid>" + "\r\n";
Xaml += @" <Grid.Resources>" + "\r\n";
Xaml += @" <my:CSampleComboBoxDataSource" + "\r\n";
Xaml += @" x:Key=""SampleComboBoxDataSource"">" + "\r\n";
Xaml += @" </my:CSampleComboBoxDataSource>" + "\r\n";
Xaml += @" </Grid.Resources>" + "\r\n";
Xaml += @" <ComboBox" + "\r\n";
Xaml += @" DataContext=""{StaticResource SampleComboBoxDataSource}""" + "\r\n";
Xaml += @" ItemsSource=""{Binding Path=SampleComboBoxDataSourceList}"" >" + "\r\n";
Xaml += @" </ComboBox>" + "\r\n";
Xaml += @" </Grid>" + "\r\n";
Xaml += @"</DataTemplate>" + "\r\n";//定义ComoboBox数据源的程序集名称
Xaml = Xaml.Replace("{AsmName}", "Test");//定义ComoboBox数据源的命名空间
Xaml = Xaml.Replace("{NameSpace}", "Test");var Rlt = XamlReader.Load(Xaml) as DataTemplate;
return Rlt;
}
}// 1)DataGrid 的虚拟数据预源
public class CSampleDataGridDataSource
{
public List<CSampleDataGridDataSourceItem> SampleDataGridDataSource
{
get
{
var Rlt = new List<CSampleDataGridDataSourceItem>();
Rlt.Add(new CSampleDataGridDataSourceItem() { CourseName = "Chinese", Score = 80.0, });
Rlt.Add(new CSampleDataGridDataSourceItem() { CourseName = "English", Score = 90.0, });
return Rlt;
}
}
}
public class CSampleDataGridDataSourceItem
{
public String CourseName { get; set; }
public Double Score { get; set; }
}// 2)ComboBox 的虚拟数据预源
public class CSampleComboBoxDataSource
{
public List<String> SampleComboBoxDataSourceList
{
get
{
var Rlt = new List<String>() { "Chinese", "English" };
return Rlt;
}
}
}
}