一、Binding是WPF的核心,WPF的数据源有以下几种:
1、ADO.NET中的DataTable
2、xml数据源
3、object数据源
4、元素控件属性
二、WPF绑定各种数据源索引
三、这里做一些简单的实例。
1.ListBox绑定Datatable数据源
前台代码
<ListBox Margin="12,32,12,329" ItemsSource="{Binding}" Name="listBox1"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.5*"/> <ColumnDefinition Width="0.5*"/> <ColumnDefinition Width="0.5*"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=ID}"/> <TextBlock Text="{Binding Path=Name}"/> <TextBlock Grid.Column="1" Text="{Binding Path=Age}" Background="{Binding Path=Age, Converter={StaticResource BackgroundConverter}}"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
后台代码:
public Window1() { InitializeComponent(); listBox1.DataContext = GetDataTable(); //使用ItemsSource的形式 //listBox1.ItemsSource = GetDataTable().DefaultView; listBox1.SelectedIndex = 0; }
下面是创建DataTable数据源,当然也可以连接数据库获得数据源
private DataTable GetDataTable() { DataTable data = new DataTable("MyDataTable"); DataColumn ID = new DataColumn("ID");//第一列 ID.DataType = System.Type.GetType("System.Int32"); //ID.AutoIncrement = true; //自动递增ID号 data.Columns.Add(ID); //设置主键 DataColumn[] keys = new DataColumn[1]; keys[0] = ID; data.PrimaryKey = keys; data.Columns.Add(new DataColumn("Name",typeof(string)));//第二列 data.Columns.Add(new DataColumn("Age",typeof(string)));//第三列 data.Rows.Add(1," XiaoM"," 20"); data.Rows.Add(2," XiaoF"," 122"); data.Rows.Add(3," XiaoA", " 29"); data.Rows.Add(4," XiaoB", " 102"); return data; }
下面是类型转换
public class BackgroundConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Color color = new Color(); int num = int.Parse(value.ToString()); if (num > 100) color = Colors.Yellow; else if (num < 50) color = Colors.LightGreen; else color = Colors.LightPink; return new SolidColorBrush(color); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }
效果图:
2、使用ListView的形式绑定DataTable数据源,数据源与第一个是一样的,不同的地方是:
前台代码:
<ListView Height="262" Margin="-12,0,12,39" VerticalAlignment="Bottom" ItemsSource="{Binding}" Name="listView2" HorizontalAlignment="Center"> <ListView.View> <GridView> <GridViewColumn Header="编号" DisplayMemberBinding="{Binding Path=ID}" Width="100" /> <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Path=Name}" Width="100"/> <GridViewColumn Header="年龄" Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Grid.Column="1" Text="{Binding Path=Age}" Foreground="{Binding Path=Age, Converter={StaticResource BackgroundConverter}}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>
后台代码:
listView2.DataContext = GetDataTable().DefaultView;
效果图:
3、如果数据源已经是DataTable,则使用LINQ检索结果的形式为:
DataTable dt = GetDataTable(); listView2.ItemsSource = from row in dt.Rows.Cast<DataRow>() where Convert.ToString(row["Name"]).StartsWith("XiaoF") select new Student() { ID = int.Parse(row["ID"].ToString()), Name = row["Name"].ToString(), Age = row["Age"].ToString() };
其它不变.上面的代码是检索Name以XiaoF开始的数据,只有一条数据。 效果图如下: