Setting an Expander toDatagrid programmatically

Alberto Sartori 1 Reputation point

I'm creating a runtime expander and I want to add to my datagrid. Here's the code:

// Creazione Expander
var expGrid = new Expander { Name = "exp" };
var expStackPanel = new StackPanel { Name = "expStackPanel", Height = 30, Orientation=Orientation.Horizontal };

// Ciclo 
foreach (var column in GridData.Columns)
    var newtextBlock = new TextBlock
        Style = this.FindResource("HeaderTextBlock") as Style,
        Width = 100,
        Text =  column.Header.ToString()
expGrid.Content = expStackPanel;
//GridData.  //?? How to?

How I've the Expander but I dunno how to add to the datagrid GridData.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points

    I will give you my sample of binding expander to DataGrid in cs code:

    xaml code:

            <local:GroupToTitleConverter x:Key="groupToTitleConverter" />
            <DataGrid x:Name="GridData" AutoGenerateColumns="False">
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True">
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}}, Converter={StaticResource ResourceKey=groupToTitleConverter}}" />
                                                    <ItemsPresenter />
                    <DataGridTextColumn Header="MyName"  Binding="{Binding MyName}"/>
                    <DataGridTextColumn Header="MyId" Binding="{Binding MyId}" />

    Cs code for binding:

     public partial class MainWindow : Window
            public MainWindow()
                ObservableCollection<MyClass> ltMyClass = new ObservableCollection<MyClass>()
                    new MyClass(){MyName="Name1",MyId="Id1"},
                    new MyClass(){MyName="Name2",MyId="Id2"},
                    new MyClass(){MyName="Name3",MyId="Id3"},
                    new MyClass(){MyName="Name4",MyId="Id3"},
                    new MyClass(){MyName="Name5",MyId="Id3"}
                ListCollectionView collection = new ListCollectionView(ltMyClass);
                collection.GroupDescriptions.Add(new PropertyGroupDescription("MyId"));
                this.GridData.ItemsSource = collection;
        public class MyClass
            public string MyName { get; set; }
            public string MyId { get; set; }
        public class GroupToTitleConverter : IValueConverter
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                GroupItem groupItem = value as GroupItem;
                CollectionViewGroup collectionViewGroup = groupItem.Content as CollectionViewGroup;
                MyClass entryViewModel = collectionViewGroup.Items[0] as MyClass;
                string title = string.Format("{0} - {1}", entryViewModel.MyName, entryViewModel.MyId);
                return title;
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                throw new NotImplementedException();
    0 comments No comments