并且,在之过程中,也使用了一些linq的知识...
以下是晚上回家自己写的,已经经过测试...
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,11,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Key}">
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23" Margin="151,11,0,0" Name="Btn_InitComboBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="101" Click="Btn_InitComboBox_Click">设置ComboBox
</Button>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,94,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" SelectedIndex="0" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Keys}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23" HorizontalAlignment="Left" Margin="151,94,0,0" Name="button1" VerticalAlignment="Top" Width="101" Click="button1_Click">Button</Button>
<Label Height="28" HorizontalAlignment="Right" Margin="0,11,122,0" Name="label1" VerticalAlignment="Top" Width="120">从IDictionary绑定</Label>
<Label Height="28" HorizontalAlignment="Right" Margin="0,94,132,0" Name="label2" VerticalAlignment="Top" Width="120">匿名对象以及ArrayList</Label>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,0,0,117" Name="comboBox3" VerticalAlignment="Bottom" Width="120" ItemsSource="{Binding}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=column1}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23" HorizontalAlignment="Left" Margin="151,0,0,117" Name="button2" VerticalAlignment="Bottom" Width="101" Click="button2_Click">Button</Button>
<Label Height="28" HorizontalAlignment="Right" Margin="0,0,122,117" Name="label3" VerticalAlignment="Bottom" Width="120">Label</Label>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,0,0,51" Name="comboBox4" VerticalAlignment="Bottom" Width="120" ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=column11}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23" Margin="151,0,0,51" Name="button3" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="101" Click="button3_Click">Button</Button>
</Grid>
后台代码:
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;
}
没什么好讲的,看代码就明白了...
以后有空,再把前台的绑定全部改成后台程序实现....
add by yanchanggang 2008-05-23 00:51:00
后台程序实现绑定以及模板选择...
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;
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,0,0,59" Name="comboBox5" VerticalAlignment="Bottom" Width="120" />
有事Q我:
姓名:颜昌钢
联系方式:yanchanggang@boco.com.cn
电话:13886086508
QQ:95550107
公司:亿阳集团武汉分公司
移动飞信:647360243