ylbtech-SilverLight-Basic-Control:基础控件使用(3)-DataGrid控件 |
DataGrid控件-后台绑定
- 自动生成表列
- 不自动生成表列
1.A,返回顶部Person类(数据源提供者) |
1,
View Code
/// <summary> /// 人员类 /// </summary> public class Person { /// <summary> /// 编号 /// </summary> public int Id { get; set; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 空参构造 /// </summary> public Person() { } /// <summary> /// 获取所有员工 /// </summary> /// <returns></returns> public static IList<Person> GetAll() { #region 人员集合数据源 IList<Person> dals = new List<Person>(); dals.Add(new Person() { Id = 101, Name = "rain", Age = 24 }); dals.Add(new Person() { Id = 102, Name = "mf", Age = 21 }); dals.Add(new Person() { Id = 103, Name = "xy", Age = 27 }); dals.Add(new Person() { Id = 104, Name = "tly", Age = 30 }); #endregion return dals; } }
4,
1.B,返回顶部 DataGrid控件绑定数据源-【AutoGenerateColumns=True】-自动生成表列 |
1,
<data:DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="108,82,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" IsEnabled="True" DataContext="{Binding}" />
2,Name,Height,Width,
AutoGenerateColumns[自动生成表列]=True|False
3,
View Code
namespace SilverlightApplication3 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); //调用方法 LoadData(); } /// <summary> /// 加载数据 /// </summary> private void LoadData() { this.dataGrid1.ItemsSource = Person.GetAll(); //绑定数据源 } } /// <summary> /// 人员类 /// </summary> public class Person { /// <summary> /// 编号 /// </summary> public int Id { get; set; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 空参构造 /// </summary> public Person() { } /// <summary> /// 获取所有员工 /// </summary> /// <returns></returns> public static IList<Person> GetAll() { #region 人员集合数据源 IList<Person> dals = new List<Person>(); dals.Add(new Person() { Id = 101, Name = "rain", Age = 24 }); dals.Add(new Person() { Id = 102, Name = "mf", Age = 21 }); dals.Add(new Person() { Id = 103, Name = "xy", Age = 27 }); dals.Add(new Person() { Id = 104, Name = "tly", Age = 30 }); #endregion return dals; } } }
4,
1.C,返回顶部DataGrid控件绑定数据源-【AutoGenerateColumns=False】-不自动生成表列 |
1,
<data:DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="108,82,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" IsEnabled="True" DataContext="{Binding}" > <data:DataGrid.Columns> <data:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="编号" Binding="{Binding Id}" IsReadOnly="True" /> <data:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="姓名" Binding="{Binding Name}" /> <data:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="年龄" Binding="{Binding Age}" /> </data:DataGrid.Columns> </data:DataGrid>
2,Name,Height,Width,
AutoGenerateColumns,Header[列头名称],Binding[{Binding 属性名称}],IsReadOnly
3,代码同上 1.B.3
4,
1.D,返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |