Search code examples
wpfdatagrid

WPF datagrid binding


I have the following domain:

public class FileInformation
{
 public String FileName;
 public String CreatedBy;  // name of user who created the file
 public String CreatedComments;
 public String CreatedDate;
 public String EditedBy;   // name of user who last edited the file
 public String EditedComments;
 public String EditedDate;
}

public class Folder
{
public List<FileInformation> Files {get;set}
}

I want to have a WPF datagrid and bind the list of Files in the "Folder" class to it .....It's pretty easy to if I want to have the data displayed in a standard way..... but I want to have it displaYed in the following way:

alt text

Any ideas on what I have to do to have the data displayed in this way ?


Solution

  • Simplest way to do it is :

    • Extract a CollectionView from the Binding:

    CollectionView cv = (CollectionView)(CollectionView)CollectionViewSource.GetDefaultView(_grid.ItemsSource);

    • Create a grouping based on FileName :

    cv.GroupDescriptions.Add(new PropertyGroupDescription("FileName"));

    • Create a GroupStyle in the Grid that displays the Gorup in the way you want it.

       <GroupStyle>
          <GroupStyle.ContainerStyle>
             <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                          <Expander IsExpanded="True" Header={Binding Name}>
                              <ItemsPresenter/>
                          </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
             </Style>
          </GroupStyle.ContainerStyle>
       </GroupStyle>
    </DataGrid.GroupStyle>
    

    The presented style will not didsplay the info exactly as presented in the screenshot but based on this tou could customize the content to match your needs...