question

AlbertoSartori-0218 avatar image
0 Votes"
AlbertoSartori-0218 asked ·

Setting an Expander toDatagrid programmatically

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

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();
         }
     }


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.