DataGrid控件-后台绑定
/// <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; } }View Code
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,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; } } }View Code
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,返回顶部 1, 2, 3, 4, 1.E,返回顶部 1, 2, 3, 4, 1.F,返回顶部 1, 2, 3, 4, 1.G,返回顶部 1, 2, 3, 4, 1.H,返回顶部 1, 2, 3, 4, 1.R,返回顶部 1, 2, 3, 4, 作者:ylbtech