Hello Team,
I am trying to set the headers for the subrows in a xamdatagrid. How can I achieve that?
Below is the source code for a sample project that has expandable rows. I have also attached the project so you can download it:
XAML
<Window xmlns:ig="http://schemas.infragistics.com/xaml" x:Class="ExapndAllMenu.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" xmlns:local="clr-namespace:ExapndAllMenu" xmlns:igDP="http://infragistics.com/DataPresenter"> <Grid> <igDP:XamDataGrid Name="xamDataGrid1" DataSource="{Binding Data}"> <igDP:XamDataGrid.FieldLayoutSettings> <!-- Set the ExpansionIndicatorDisplayMode so that when a record is displayed it will be checked to see if it has any child records. The expansions indicator will only be displayed if the record has children. --> <igDP:FieldLayoutSettings ExpansionIndicatorDisplayMode="CheckOnDisplay"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.Resources> <local:ShouldDisplayMenuConverter x:Key="ShouldDisplayMenu"/> <Style TargetType="{x:Type igDP:HeaderPrefixArea}">
<Setter Property="Template"> <Setter.Value> <ControlTemplate> <ig:XamMenu Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}, Converter={StaticResource ShouldDisplayMenu}}"> <ig:XamMenuItem Header="!"> <ig:XamMenuItem Header="Expand All" Click="XamMenuItem_Click"/> </ig:XamMenuItem> </ig:XamMenu> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:XamDataGrid.Resources> </igDP:XamDataGrid> <Button Content="Expand First Record" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10" Click="Button_Click"/> </Grid></Window>
C#
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;using Infragistics.Windows.DataPresenter;using System.Collections.ObjectModel;
namespace ExapndAllMenu{ /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent();
Data = new ObservableCollection<Data>();
for (int i = 0; i < 10; i++) { Data d = new Data { Children = new ObservableCollection<Data>(), ID = i, Name = "Parent " + i };
if (i != 4) for (int j = 0; j < 5; j++) { Data d1 = new Data { Children = new ObservableCollection<Data>(), ID = j, Name = "Child " + i + j };
for (int p = 0; p < 5; p++) { d1.Children.Add(new Data { ID = p, Name = "Child " + j + p }); }
d.Children.Add(d1); }
Data.Add(d); }
DataContext = this; }
public ObservableCollection<Data> Data { get; set; }
private void XamMenuItem_Click(object sender, EventArgs e) { xamDataGrid1.Records.ExpandAll(true); }
private void Button_Click(object sender, RoutedEventArgs e) { xamDataGrid1.Records[0].IsExpanded = true; } }
public class ShouldDisplayMenuConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DataRecordPresenter rec = value as DataRecordPresenter; if (rec != null) { if ((rec.Record as HeaderRecord).AttachedToRecord.ParentRecord == null) return Visibility.Visible; }
return Visibility.Collapsed; }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
public class Data { public int ID { get; set; } public string Name { get; set; } public ObservableCollection<Data> Children { get; set; } }}
Thank you.
Hello Krasimir,
Please ignore the last question.
Best regards,
Sherif
sorry for the late reply. Is it possible to have different headers for different subrows. Can I assign the headers in each subrow dynamically?
Hello Sherif,
I am just checking if you require any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Thank you for your post. I have been looking into it and if I understand correctly, you wish to have a menu in each HeaderRecord of the XamDataGrid and using it, to allow expanding the records that are related to the HeaderRecord. If this is correct, I can suggest using the DataRecordPresenter to get the HeaderRecord, on the click of the XamMenuItem and using the HeaderRecord’s AttachedToRecord property to get the RecordCollection, which should be expanded. I have modified the sample application that you have attached, in order to show this approach. I have used a command behavior and a static command to handle the click of the XamMenuItem, in order to implement the approach using the MVVM pattern.
Please let me know it you need any further assistance on the matter.