强大的DataGrid组件[9]_自定义头模板(HeaderTemplate)——Silverlight学习笔记[17]

原文地址:http://www.cnblogs.com/Kinglee/archive/2009/08/27/1555141.html

在DataGrid的开发设计中,我们经常会碰到设计样式各异的表头以满足各种要求。而头模板的作用是显示DataGrid控件的首行中的文本、图片或是绑定数据的。通过对头模板的设定,可以为我们定制所需样式的DataGrid。本文将为大家介绍如何自定义DataGrid的头模板。

 

具体步骤

1)在XAML文件中的UserControl标签中加入如下命名空间:

xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"

 

2)设置呈现头模板的样式HeaderStyle的属性

 

3)可以利用StackPanel标签组合编排添加在头模板内的组件的位置。

 

 

实例

通过实例来了解头模板制定的基本方法。

先来看看效果:

 


在代码中会指明操作的关键步骤。

MainPage.xaml文件代码

<UserControl

    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:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"

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

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

    <Grid x:Name="LayoutRoot" Width="640" Height="320" Background="White">

        <data:DataGrid x:Name="dgEmployee" AutoGenerateColumns="False" Margin="8,8,36,71" Background="#FFDEF2F0" FontSize="12">

            <data:DataGrid.Columns>

                <data:DataGridTemplateColumn Width="260">

                    <data:DataGridTemplateColumn.HeaderStyle>

                        <Style TargetType="dataprimitives:DataGridColumnHeader">

                            <Setter Property="ContentTemplate">

                                <Setter.Value>

                                    <DataTemplate>

                                        <!--呈现的关键-->

                                        <StackPanel Orientation="Vertical">

                                            <StackPanel Orientation="Horizontal">

                                                <TextBlock Text="" Width="80"/>

                                                <TextBlock Text="   2009-07" Width="80"/>

                                                <TextBlock Text="" Width="100"/>

                                            </StackPanel>

                                            <StackPanel Orientation="Horizontal">

                                                <TextBlock Text="单价" Width="80"/>

                                                <TextBlock Text="数量" Width="80"/>

                                                <TextBlock Text="总额" Width="100"/>

                                            </StackPanel>

                                        </StackPanel>

                                    </DataTemplate>

                                </Setter.Value>

                            </Setter>

                        </Style>

                    </data:DataGridTemplateColumn.HeaderStyle>

                    <!--这里用到了我上一篇中提到的方法-->

                    <data:DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                          <StackPanel Orientation="Horizontal">                                                 

                            <TextBlock Width="80" Text="{Binding Quantity}"/>

                            <TextBlock Width="80" Text="{Binding Price}"/>

                            <TextBlock Width="100" Text="{Binding Total}"/>

                          </StackPanel>

                        </DataTemplate>

                    </data:DataGridTemplateColumn.CellTemplate>

                    <data:DataGridTemplateColumn.CellEditingTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <TextBox Width="80" Text="{Binding Quantity,Mode=TwoWay}"/>

                                <TextBox Width="80" Text="{Binding Price,Mode=TwoWay}"/>

                                <TextBox Width="100" Text="{Binding Total,Mode=TwoWay}"/>

                            </StackPanel>

                        </DataTemplate>

                    </data:DataGridTemplateColumn.CellEditingTemplate>

                </data:DataGridTemplateColumn>

            </data:DataGrid.Columns>

        </data:DataGrid>

    </Grid>

</UserControl>

 

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 Products

    {

        public int Quantity { get; set; }

        public int Price { get;set;}

        public int Total { get; set; }

    }

 

    public partial class MainPage : UserControl

    {

       

        public MainPage()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            List<Products> em1 = new List<Products>();

            em1.Add(new Products() { Quantity = 20, Price = 130, Total = 2600 });

            em1.Add(new Products() { Quantity = 5, Price=800, Total = 4000 });

            em1.Add(new Products() { Quantity = 10, Price=2000, Total = 20000 });

           

            dgEmployee.ItemsSource = em1;

        }

    }

}

作者:Kinglee
文章出处:Kinglee’s Blog ( http://www.cnblogs.com/Kinglee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。
好的,下面是一个带ComboBox表DataGrid模板: ```xaml <Window x:Class="ComboBoxHeaderDataGridSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ComboBox Header DataGrid Sample" Height="350" Width="525"> <Grid> <DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="ComboBox Column"> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <ComboBox Width="100" ItemsSource="{Binding DataContext.ComboBoxItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" SelectedValue="{Binding DataContext.SelectedComboBoxItem, RelativeSource={RelativeSource AncestorType=DataGrid}}, Mode=TwoWay}" DisplayMemberPath="Text" SelectedValuePath="Value"/> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding ComboBoxItem.Text}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Text Column" Binding="{Binding Text}"/> </DataGrid.Columns> </DataGrid> </Grid> </Window> ``` 这个模板中包含了一个DataGrid,其中有两列,一列是ComboBox列,另一列是普通的Text列。ComboBox列的表是一个ComboBox,它的ItemsSource绑定到了DataGrid的DataContext中的ComboBoxItems属性,SelectedValue绑定到了SelectedComboBoxItem属性。当用户选择ComboBox项时,SelectedComboBoxItem属性的值会更新,而当DataGrid的DataContext中的ComboBoxItems属性的值发生变化时,ComboBox中的项也会随之变化。Text列的表是一个普通的TextBlock,它的Text绑定到了数据项的Text属性,用来显示文本。 在C#代码中,我们需要为DataGrid设置DataContext,以及为ComboBox列提供ComboBoxItems和SelectedComboBoxItem属性的值。下面是一个例子: ```csharp using System.Collections.Generic; using System.Windows; namespace ComboBoxHeaderDataGridSample { public partial class MainWindow : Window { public List<ComboBoxItem> ComboBoxItems { get; set; } // ComboBox列的ComboBoxItems属性 public ComboBoxItem SelectedComboBoxItem { get; set; } // ComboBox列的SelectedComboBoxItem属性 public MainWindow() { InitializeComponent(); // 初始化ComboBoxItems ComboBoxItems = new List<ComboBoxItem> { new ComboBoxItem { Text = "Item 1", Value = 1 }, new ComboBoxItem { Text = "Item 2", Value = 2 }, new ComboBoxItem { Text = "Item 3", Value = 3 } }; // 初始化SelectedComboBoxItem SelectedComboBoxItem = ComboBoxItems[0]; // 设置DataContext DataContext = this; // 添加数据项 dataGrid.Items.Add(new DataItem { ComboBoxItem = ComboBoxItems[0], Text = "Text 1" }); dataGrid.Items.Add(new DataItem { ComboBoxItem = ComboBoxItems[1], Text = "Text 2" }); dataGrid.Items.Add(new DataItem { ComboBoxItem = ComboBoxItems[2], Text = "Text 3" }); } } public class ComboBoxItem { public string Text { get; set; } public int Value { get; set; } } public class DataItem { public ComboBoxItem ComboBoxItem { get; set; } public string Text { get; set; } } } ``` 在这个例子中,我们创建了两个类,一个是ComboBoxItem类,用来表示ComboBox中的每一个项,另一个是DataItem类,用来表示每一行数据。在MainWindow的构造函数中,我们为ComboBoxItems和SelectedComboBoxItem属性提供了初始值,并将MainWindow本身设置为DataContext。然后我们添加了三个DataItem对象到DataGrid中,每个对象中都包含了一个ComboBoxItem和一个Text属性,用来填充ComboBox列和Text列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值