воскресенье, 6 февраля 2011 г.

DataGrid groupping snippet


What we should get:



XAML:
<Window x:Class="WpfDataGrid1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Margin="15,40,14,12" Name="dataGrid1">
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style  TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander x:Name="exp" IsExpanded="True"
                                            Background="White"
                                            Foreground="Black">
                                        <Expander.Header>
                                            <TextBlock Text="{Binding Name}"/>
                                        Expander.Header>
                                        <ItemsPresenter />
                                    Expander>
                                ControlTemplate>
                            Setter.Value>
                        Setter>
                        Style>
                    GroupStyle.ContainerStyle>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        ItemsPanelTemplate>
                    GroupStyle.Panel>
                GroupStyle>
            DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Data2" Binding="{Binding Path=MyItemValue}" Width="*">DataGridTextColumn>
            DataGrid.Columns>
        DataGrid>
    Grid>
Window>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDataGrid1
{
    ///

    /// Interaction logic for MainWindow.xaml
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<MyItem> MyItems = new List<MyItem>
                {
                    new MyItem{ MyItemName= "US", MyItemValue=77 },
                    new MyItem{ MyItemName= "US", MyItemValue=778 },
                    new MyItem{ MyItemName= "US", MyItemValue=78 },
                    new MyItem{ MyItemName= "UK", MyItemValue=7 },
                    new MyItem{ MyItemName= "Israel", MyItemValue=9 },
                    new MyItem{ MyItemName= "Turkey", MyItemValue=98 },
                    new MyItem{ MyItemName= "Turkey", MyItemValue=4 }
                };

            CollectionViewSource collection = new CollectionViewSource();
            collection.Source = MyItems;
            PropertyGroupDescription propertyGroupDescription = new PropertyGroupDescription("MyItemName");
            collection.GroupDescriptions.Add(propertyGroupDescription);
            dataGrid1.ItemsSource = collection.View;
        }
    }
}

Комментариев нет:

Отправить комментарий