Setting an Expander toDatagrid programmatically

Alberto Sartori 1 Reputation point
2020-06-08T08:28:31.62+00:00

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()
    };
    expStackPanel.Children.Add(newtextBlock);
}
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
    2020-06-09T03:52:36.11+00:00

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

    xaml code:

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

    Cs code for binding:

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                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