VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql
1、参考以前博文,恢复在 Vs2010+linQ for Mysql的环境。
2、建立 wpf工程,参照1,生成 datacontext.cs ,并拷入几个动态库、DBHelper.cs ,添加程序配置文件 app.config,并参照1例子工程进行修改;
3、参考以下博文数据源之 Datatable,并利用DBHelper类的GetDataTableBySql方法,生成GetDataTable(),分别实践 listbox 、listview两种绑定方法。
4、继续修改为 linq for mysql 的 绑定方法
前台不变;
后台把数据源整合为 test()
private void test()
{
using (MySqlConnection conn = new MySqlConnection(dbServer))
{
Equipment db = new Equipment(conn);
var q = (from i in db.Hardware
select new { i.Model, i.Name, i.Site }).ToList();
listView2.DataContext = q;
}
}
运行结果与上面相同;
http://www.cnblogs.com/linlf03/archive/2011/09/06/2168440.html
一、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开始的数据,只有一条数据。 效果图如下: