强大的DataGrid组件[7]_自定义DataGrid

来源:百度文库 编辑:神马文学网 时间:2024/04/28 22:41:55

今天我们来一起学习怎样自定义DataGrid的单元格呈现形式的基本操作方法。

 

基本知识讲解

 

1)两种状态

DataGrid的单元格的状态有两类,即编辑状态和非编辑状态。

在实际开发中,如果一个单元格所在的列不设为只读的话(即要求可读写),那么这个单元格就存在这两种状态。按需要,这时就应当对这两种状态对单元格分别设定不同的编辑模板。如果该单元格仅用于进行简易的文本编辑,则可保留原有状态,无需重新设定。

这两种编辑模板的标签如下所示:

①非编辑状态模板:

②编辑状态模板:

 

2)三种模板

普通文本列,即基本默认设置

带有复选框的列,当该列单元格数据的值为true或false、1或0时,将该列的模板设定。

自定义模板列,这个是功能最强的可以放入任何自定义控件。

 

 

关于绑定

如果单元格所在列无需编辑或只读的话,绑定模式设定为默认的OneWay即可。如果该列需要进行编辑,就请将绑定模式设为TwoWay。

更为详细的说明请参见MSDN的文章。(点这里)

 

 

实例

说明:为了能自定义列,我们需要先将DataGrid的AutoGenerateColumns属性设为false。

 

MainPage.xaml文件代码

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:src="clr-namespace:SilverlightClient"

    mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage"

    d:DesignWidth="320" d:DesignHeight="240">

   

  

       

       

           

               

                

               

               

                   

                       

                           

                       

                    

                   

                       

                           

                       

                   

               

               

           

       

   

MainPage.xaml.cs文件代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace SilverlightClient

{

    //静态资源绑定源

    public class cbSexListProvider

    {

        public List cbSexList

        {

            get

            {

                return new List { "男", "女" };

            }

        }

    }

 

    //定义数据类

    public class Employees

    {

        public int EmployeeID { get; set; }

        public string EmployeeName { get; set; }

        public int EmployeeAge { get; set; }

        public string EmployeeSex { get; set; }

        public int EmployeeMarried { get; set; }

    }

 

    public partial class MainPage : UserControl

    {

        List em = new List();

 

        public MainPage()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            em.Add(new Employees() { EmployeeID = 1, EmployeeName = "张三", EmployeeAge = 23, EmployeeSex = "男", EmployeeMarried = 0 });

            em.Add(new Employees() { EmployeeID = 2, EmployeeName = "李四", EmployeeAge = 24, EmployeeSex = "女", EmployeeMarried = 1 });

            em.Add(new Employees() { EmployeeID = 3, EmployeeName = "王五", EmployeeAge = 25, EmployeeSex = "男", EmployeeMarried = 1 });

            dgCustom.ItemsSource = em;

        }

    }

}

 

最终效果图: