private void Btn_InitComboBox_Click(object sender, RoutedEventArgs e)
{
IDictionary<string, string> ComboxItems = new Dictionary<string, string>();
for (int i = 0; i < 10; i++)
ComboxItems.Add(i.ToString(), i.ToString());
this.comboBox1.DataContext = ComboxItems;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ArrayList arrList = new ArrayList();
for (int i = 0; i < 10; i++)
{
arrList.Add(new { Keys=i.ToString(),Values=(i+1).ToString()});
}
this.comboBox2.DataContext = arrList;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("column1");
DataColumn col2 = new DataColumn("column2");
dt.Columns.Add(col1);
dt.Columns.Add(col2);
for (int i = 0; i < 10; i++)
{
DataRow row = dt.NewRow();
row[0] = i.ToString();
row[1] = i.ToString();
dt.Rows.Add(row);
}
this.comboBox3.DataContext = dt;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("column1");
DataColumn col2 = new DataColumn("column2");
dt.Columns.Add(col1);
dt.Columns.Add(col2);
for (int i = 0; i < 10; i++)
{
DataRow row = dt.NewRow();
row[0] = i.ToString();
row[1] = i.ToString();
dt.Rows.Add(row);
}
IEnumerable temp = from row in dt.AsEnumerable()
where row.Field<string >("column1")!="1"
select new { column11 = row.Field<string>("column1"), column22 = row.Field<string>("column2") };
this.comboBox4.DataContext = temp;
}
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("column1");
DataColumn col2 = new DataColumn("column2");
dt.Columns.Add(col1);
dt.Columns.Add(col2);
for (int i = 0; i < 10; i++)
{
DataRow row = dt.NewRow();
row[0] = i.ToString();
row[1] = i.ToString();
dt.Rows.Add(row);
}
IEnumerable temp = from row in dt.AsEnumerable()
where row.Field<string>("column1") != "1"
select new { column11 = row.Field<string>("column1"), column22 = row.Field<string>("column2") };
DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(Label));//创建一个控件模板
Binding binding = new Binding();
binding.Path = new PropertyPath("column11");//绑定到哪个字段
frameworkElementFactory.SetBinding(ContentProperty, binding);//设置绑定到控件的哪个属性...
dataTemplate.VisualTree = frameworkElementFactory;
this.comboBox5.ItemTemplate = dataTemplate;
this.comboBox5.ItemsSource = temp;