Binding Datagrid extended with toolbar

Feb 12, 2010 at 5:08 PM

Hi All,

I'm trying to extend datagrid, adding a toolbar on the top, with buttons to exports data to excel or print it. AFAIK is not a builin option of datagrid.

What I've made is to create a custom control

public class DataGridPlus : DataGrid
         static DataGridPlus()
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridPlus), new FrameworkPropertyMetadata(typeof(DataGridPlus)));


added a generic.xaml file with the design of the toolbar

<Style TargetType="{x:Type local:DataGridPlus}">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type local:DataGridPlus}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                                <RowDefinition Height="35"/>
                                <RowDefinition Height="*"/>

                                <ColumnDefinition Width="*"/>

                            <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5">
                                <Button  x:Name="PART_ExportarExcel"  ToolTip="Exportar a Excel" HorizontalAlignment="Left" >
                                    <Image Source="/OrcaCP;component/Resources/Img/spreadsheet_document.png" Height="16"/>
                                <Button  x:Name="PART_Imprimir" ToolTip="Imprimir" HorizontalAlignment="Left" Margin="5,0,0,0" >
                                    <Image Source="/OrcaCP;component/Resources/Img/agt_print.png" Height="16"/>

                            <dg:DataGrid Grid.Row="1" Grid.Column="0"  x:Name="PART_DataGrid"
                                                Width="{TemplateBinding Width}"
                                                DataContext="{TemplateBinding DataContext}"
                                                IsReadOnly ="{TemplateBinding IsReadOnly}"
                                                CanUserAddRows ="{TemplateBinding CanUserAddRows}"
                                                CanUserDeleteRows ="{TemplateBinding CanUserDeleteRows}"
                                                CanUserReorderColumns ="{TemplateBinding CanUserReorderColumns}"
                                                CanUserResizeRows ="{TemplateBinding CanUserResizeRows}"
                                                CanUserSortColumns ="{TemplateBinding CanUserSortColumns}"
                                                AllowDrop="{TemplateBinding AllowDrop}"
                                                SelectionUnit="{TemplateBinding SelectionUnit}"
                                                AutoGenerateColumns="{TemplateBinding AutoGenerateColumns}"




and implemented the funcionality of the tolbar buttons:

public override void OnApplyTemplate()

           // base.DataContext=DataGridPlus.DataContextProperty

            Button Export2ExcelButton = base.GetTemplateChild("PART_ExportarExcel") as Button;
            Button PrintButton = base.GetTemplateChild("PART_Imprimir") as Button;

            DataGrid dg = base.GetTemplateChild("PART_DataGrid") as DataGrid;


            if (Export2ExcelButton != null)
                Export2ExcelButton.Click += new RoutedEventHandler(Export2ExcelButton_Click);

            if (PrintButton != null)
                PrintButton.Click += new RoutedEventHandler(PrintButton_Click);

Up to here, everything worked well. The problem, and I think is a concept problem (my fault), is that when I define the Datagrid in my template, i have to pass explicity all the TemplateBinding, and I  don't know how to deal with column definitions: it only works with autogeneratecolumns = true


Any ideas?. Is this the best way to customize the control?.

Thanks in advance.