Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
100
XamDataGrid Custom ControlTemplate
posted

I would need to create a Xamdatagrid control which contains buttons in the header that perform certain inertial grid operations. For example, export dates in excel, save to column configuration, import a configuration for columns, and more. Since I often use these methods in my programs, I need to create this control so I do not have to rewrite every code every time.
To do this, I created a Custom Control and inserted the following code

for Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:HHXamDataGrid"
    xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf">
    <Style TargetType="{x:Type local:HHXamDataGrid}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:HHXamDataGrid}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="42"/>
                                <RowDefinition Height="62*"/>
                            </Grid.RowDefinitions>
                            <Button x:Name="btn_Open" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Width="32" Height="32" ToolTip="Apri configurazione griglia">
                                <Image Source="Images/open.png" ToolTip="Apri configurazione griglia"/>
                            </Button>
                            <Button x:Name="btn_Save" HorizontalAlignment="Left" Margin="50,5,0,0" VerticalAlignment="Top" Width="32" Height="32" ToolTip="Salva configurazione griglia">
                                <Image  Source="Images/save.png" ToolTip="Salva configurazione griglia" />
                            </Button>
                            <Button x:Name="btn_ExportExcel" HorizontalAlignment="Left" Margin="90,5,0,0" VerticalAlignment="Top" Width="32" Height="32" ToolTip="Esporta griglia in formato Excel">
                                <Image  Source="Images/excel.png" ToolTip="Esporta griglia in formato Excel" />
                            </Button>
                            <igWPF:XamDataGrid HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="1" VerticalAlignment="Stretch" Width="Auto" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary

While this is the code of my custom control

Public Class HHXamDataGrid
    Inherits Infragistics.Windows.DataPresenter.XamDataGrid
 
    Shared Sub New()
        'Questa chiamata OverrideMetadata indica al sistema che l'elemento fornisce uno stile diverso dalla relativa classe base.
        'Questo stile viene definito in Themes\Generic.xaml
        DefaultStyleKeyProperty.OverrideMetadata(GetType(HHXamDataGrid), New FrameworkPropertyMetadata(GetType(HHXamDataGrid)))
    End Sub
 
    Private _btn_Open As Button
    Private _btn_Save As Button
    Private _btn_Export As Button
 
    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()
 
        _btn_Open = Template.FindName("btn_Open"Me)
        _btn_Save = Template.FindName("btn_Save"Me)
        _btn_Export = Template.FindName("btn_ExportExcel"Me)
 
        If _btn_Export Is Nothing Or _btn_Open Is Nothing Or _btn_Save Is Nothing Then
            Throw New Exception("Controlli fondamentali non trovati nel template")
        End If
 
        AddHandler _btn_Open.Click, AddressOf Me.btn_Open_Click
        AddHandler _btn_Save.Click, AddressOf Me.btn_Save_Click
        AddHandler _btn_Export.Click, AddressOf Me.btn_ExportExcel_Click
 
    End Sub
 
 
    Private Sub btn_Open_Click(sender As Object, e As RoutedEventArgs)
        MessageBox.Show("Open Button Click")
    End Sub
 
    Private Sub btn_Save_Click(sender As Object, e As RoutedEventArgs)
        MessageBox.Show("Save Button Click")
    End Sub
 
    Private Sub btn_ExportExcel_Click(sender As Object, e As RoutedEventArgs)
        MessageBox.Show("Export Button Click")
    End Sub
 
End Class

The point is that the buttons work properly while I try to add fields from the code xaml, like this

<igDP:XamDataGrid.FieldLayoutSettings>
                        <igDP:FieldLayoutSettings  AllowAddNew="False" AllowDelete="False" AutoGenerateFields="False"/>
                    </igDP:XamDataGrid.FieldLayoutSettings>
                    <igDP:XamDataGrid.FieldSettings>
                        <igDP:FieldSettings AllowRecordFiltering="True" AllowSummaries="True" SummaryDisplayArea="TopFixed">
                        </igDP:FieldSettings>
                    </igDP:XamDataGrid.FieldSettings>
                    <igDP:XamDataGrid.FieldLayouts>
                        <igDP:FieldLayout>
                            <igDP:Field Name="name" >
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="False"/>
                                </igDP:Field.Settings>
                            </igDP:Field>
                            <igDP:Field Name="surname" >
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="True"/>
                                </igDP:Field.Settings>
                            </igDP:Field>
                        </igDP:FieldLayout>
                    </igDP:XamDataGrid.FieldLayouts>

the XamDataGrid don't work

Where am I wrong?
How can I do this to create my own control so I can reuse it in the various parts of my program?
Thank you for your help.