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.
Thank you very much.
My mistake was that from design Xaml I did not see any column. But I realized that as soon as you set the xamdatagrid datasource, the fieldsettings are set.
Very Very Very Thanks
Hello Francesco,
Thank you for the sample you have sent, it was very helpful to observe the functionality you want to achieve.
If you want to set the fields explicitly yourself, rather than letting the XamDataGrid Autogenerate them you will have to turn that option off, e.g.:
<MyWpfXamDataGrid:MyCustomXDataGrid.FieldLayoutSettings> <Custom:FieldLayoutSettings AutoGenerateFields="False"/> </MyWpfXamDataGrid:MyCustomXDataGrid.FieldLayoutSettings>
Additionally you will also have to provide some data source for them. For example if you plan to use ObservableCollection for your data you can bind it in the grid's data source:DataSource="{Binding MyData}"
You can take a look at this series of topics:https://ko.infragistics.com/help/wpf/XamDataGrid
If you have further questions concerning this matter please let me know.
In the project that I have attach I've created in xaml codes fieldlayouts and 2 field but these are not displayed is as if I had not created them.
Where i'm wrong?
I've created the wpf custom controls project with new Generic.xaml.
I attach the solution with custom control and project test
MyWpfXamDataGrid.zip
Thank you for the code snippet.
If possible add some details of your scenario:Are you modifying the default styles, which are located at:C:\Program Files (x86)\Infragistics\2017.1\WPF\DefaultStyles\DataPresenter. DataPresenterGeneric.xamlOr you are creating your own Generic.xaml?
Did the buttons as you described worked before adding the fields?If possible add the application as an attached zip file to this post.
Having this information will help me to further investigate this for you.