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;

        }

posted @ 2008-12-05 14:21  昕友软件开发  阅读(4002)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯