I have a window with multiple XamGrids but I am going to bind the same data structure to each of them. What is a good way to define the column layout once and then reference that layout with each of the grids? I would like to avoid maintaining multiple versions of the same column layout. For example, the following is from your samples:
<ig:XamGrid x:Name="dataGrid" AutoGenerateColumns="false" ItemsSource="{Binding Path=Customers}" ColumnWidth="*"> <ig:XamGrid.Columns> <ig:TextColumn Key="CustomerID"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_CustomerID, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> <ig:TextColumn Key="Company"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_Company, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> <ig:TextColumn Key="ContactName"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_ContactName, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> <ig:TextColumn Key="ContactTitle"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_ContactTitle, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> <ig:TextColumn Key="Region"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_Region, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> <ig:TextColumn Key="Country"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Customers_Country, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn> </ig:XamGrid.Columns> </ig:XamGrid>
<ig:XamGrid x:Name="dataGrid" AutoGenerateColumns="false" ItemsSource="{Binding Path=Customers}" ColumnWidth="*">
<ig:XamGrid.Columns>
<ig:TextColumn Key="CustomerID">
<ig:TextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=XWG_Customers_CustomerID, Source={StaticResource Strings}}" />
</DataTemplate>
</ig:TextColumn.HeaderTemplate>
</ig:TextColumn>
<ig:TextColumn Key="Company">
<TextBlock Text="{Binding Path=XWG_Customers_Company, Source={StaticResource Strings}}" />
<ig:TextColumn Key="ContactName">
<TextBlock Text="{Binding Path=XWG_Customers_ContactName, Source={StaticResource Strings}}" />
<ig:TextColumn Key="ContactTitle">
<TextBlock Text="{Binding Path=XWG_Customers_ContactTitle, Source={StaticResource Strings}}" />
<ig:TextColumn Key="Region">
<TextBlock Text="{Binding Path=XWG_Customers_Region, Source={StaticResource Strings}}" />
<ig:TextColumn Key="Country">
<TextBlock Text="{Binding Path=XWG_Customers_Country, Source={StaticResource Strings}}" />
</ig:XamGrid.Columns>
</ig:XamGrid>
Assuming I have this exact same layout for 5 different grids on the window, how would I allow each grid to share this layout?
Thanks!
Hello elondon ,
I have been looking into your question and I can suggest you use a separate UserControl where you can extend our XamDataGrid with the fieldlayout that you want. Therefore you can provide the same user control to all of the places in your application when you need the customized XamDataGrid control.
If you need any additional assistance with this matter please feel free to ask.
That is certainly a solution I had considered - I was more curious if there was a way to do it as some kind of template in the XAML. As in define some kind of column layout template and tell each grid to use that for its column layout. The user control will do, though.
Hello elondon,
Currently it is possible to add your columns in a ColumnLayout and move the ColumnLayout definition to your Resources section. Then you can handle the Loaded event of the XamGrid and assign the layout to it as follows:
private void xamGrid1_Loaded(object sender, RoutedEventArgs e)
{
xamGrid1.ColumnLayouts.Add(Application.Current.MainWindow.Resources["Clayout"] as ColumnLayout);
}
in this snippet Clayout e x:key of the resource. Still please have in mind that this is just a custom approach and it may need some customization depending on your scenario.
Please let me know if you have any further questions or concerns with this matter.