Silverlight中的DataGrid绑定数据

 

首先写一个类:
public class Employee
 {
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public decimal Salary { get; set; }
  public DateTime StartDate { get; set; }
  public bool IsVested { get; set; }
  public string Gender { get; set; }
 }

接着再写一个类,用于DataGrid性别列的下拉框。

public class Genders
    {
        public List<String> GenderList
        {
            get
            {
                return new List<String> { "Male", "Female" };
            }
        }
    }

加入一个名称空间:
xmlns :local="clr-namespace:DataGrid_cs"

然后声明一个属性引用:
<UserControl.Resources>
<local:Genders x:Key="genderList"></local:Genders>
</UserControl.Resources>


编写DataGrid代码:
<data:DataGrid x:Name="grdDisplay"
      IsReadOnly="False"
        AutoGenerateColumns="False"
        RowBackground="Beige"
        AlternatingRowBackground="BlanchedAlmond"
        ColumnWidth="100"
        GridLinesVisibility="Horizontal"
        HorizontalGridLinesBrush="Blue"
        HeadersVisibility="Column"
        Margin="0,50,0,0"
        >
   <data:DataGrid.Columns>
    <data:DataGridTextColumn Header="First Name" CanUserSort="False" Binding="{Binding FirstName}" />
    <data:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
    <data:DataGridTextColumn Header="Salary" Binding="{Binding Salary}" />
    <data:DataGridCheckBoxColumn Header="Is Vested" Binding="{Binding IsVested}" />
    <data:DataGridTemplateColumn Header="Start Date" SortMemberPath="StartDate">
     <data:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
       <TextBlock Text="{Binding Path=StartDate}" />
      </DataTemplate>
     </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>
    <data:DataGridTemplateColumn Header="Gender" SortMemberPath="Gender">
     <data:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
       <TextBlock Text="{Binding Path=Gender}" />
      </DataTemplate>
     </data:DataGridTemplateColumn.CellTemplate>
                    <data:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Loaded="ComboBox_Loaded" SelectedItem="{Binding Path=Gender, Mode=TwoWay}" ItemsSource="{Binding GenderList, Source={StaticResource genderList}}"></ComboBox>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellEditingTemplate>
                </data:DataGridTemplateColumn>
   </data:DataGrid.Columns>
  </data:DataGrid>

最后写后台代码:

public Page()
  {
   InitializeComponent();
   List<Employee> employeelist = new List<Employee>();
   employeelist.Add(new Employee { FirstName = "Tom", LastName = "Smith", Salary = 50000, StartDate = new DateTime(2006, 10, 3), IsVested = true, Gender = "Male" });
   employeelist.Add(new Employee { FirstName = "Sue", LastName = "Smith", Salary = 45000, StartDate = new DateTime(2006, 10, 4), IsVested = true, Gender = "Female" });
   employeelist.Add(new Employee { FirstName = "Sharon", LastName = "Smith", Salary = 79000, StartDate = new DateTime(2006, 10, 5), IsVested = false, Gender = "Female" });
   employeelist.Add(new Employee { FirstName = "Joe", LastName = "Smith", Salary = 55000, StartDate = new DateTime(2006, 10, 6), IsVested = true, Gender = "Male" });
   employeelist.Add(new Employee { FirstName = "John", LastName = "Smith", Salary = 60000, StartDate = new DateTime(2006, 10, 7), IsVested = false, Gender = "Male" });
   employeelist.Add(new Employee { FirstName = "Amy", LastName = "Smith", Salary = 65000, StartDate = new DateTime(2006, 10, 8), IsVested = true, Gender = "Female" });
   employeelist.Add(new Employee { FirstName = "Kristi", LastName = "Smith", Salary = 150000, StartDate = new DateTime(2006, 10, 9), IsVested = false, Gender = "Female" });
   grdDisplay.ItemsSource = employeelist;

  }

        private void ComboBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ((ComboBox)sender).IsDropDownOpen = true;

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值