综合应用WPF/WCF/WF/LINQ之三十八:实现一个简单的DataGrid之整体介绍

为了方便读者学习,请 单击这里下载源代码。
  首先,咱们来看一下使用这个控件后的效果图:

  咱们再来看看实现这个控件的XAML代码:
    1  < common : DataGrid Name ="grdDataGrid" ItemsSource ="{ Binding }" CheckBoxChecked ="grdDataGrid_CheckBoxChecked" ButtonClick ="grdDataGrid_ButtonClick" ComboBoxSelectionChanged ="grdDataGrid_ComboBoxSelectionChanged">
    2      < ListView.View >
    3          < GridView >
    4              < common : DataGridColumn Header ="First Name" ColumnType ="TextBlock" TextBlockTextPath ="LastName" ButtonValuePath ="ID" />
    5              < common : DataGridColumn Header ="Last Name" ColumnType ="ComboBox" ComboBoxTextPath ="LastName" ComboBoxDisplayMemberPath ="LastName" ComboBoxSelectedValuePath ="ID" HasDifferentDataContext ="True" SortPropertyName ="LastName" />
    6              < common : DataGridColumn Header ="ID" ColumnType ="CheckBox" CheckBoxValuePath ="ID" SortPropertyName ="ID" Width ="50" />
    7              < common : DataGridColumn Header ="Date of Birth" TextBlockTextPath ="DateOfBirth" TextBlockTextAlignment ="Right" SortPropertyName ="DateOfBirth" IsDefaultSortColumn ="True" />
    8          </ GridView >
    9      </ ListView.View >
   10  </ common : DataGrid >
  咱们能够看到,使用这个控件跟普通的ListView控件没有太多的区别。咱们只须要在DataGridColumn中指定ColumnType为Button、CheckBox、ComboBox、TextBox等,默认为TextBlock,而后对应该类型设定相应的属性便可。而相关的排序、DataTemplate等功能则已经彻底封装了。另外,普通的ListView控件的RoutedEventArgs中的信息太少,而取得SourceRowIndex、SourceColumnIndex、CurrentRowIndex、CurrentColumnIndex等则又很难实现,且很是重要,因此笔者也实现了本身的RoutedEventArgs,提供上述各类Index,方便你们在该控件的事件中直接使用。   下面的几篇文章我将对该控件中的各个重要方面进行一一讲解。
相关文章
相关标签/搜索