Search code examples
wpfdatagriditemscontrol

Datagrid row with multiple items


I have a list of items and each item contains a list of sub items. I am trying to display these in an Xceed datagrid with one row per item, but if there are more than one sub item I want to add these in vertical stack panels in the same row, in the corresponding columns. The data grid should look like this (ignore the dashes, i used them to align the text):

----------ID-------DateIn---------DateOut
row 1 --ID1----10/02/2012 --11/02/2012

row 2 --ID2----10/03/2012 --11/03/2012

------------------11/03/2012 --12/03/2012

------------------12/03/2012--13/03/2012

row 3 --ID3 ----11/03/2012 --12/03/2012

the current code is below, it displays only the first sub item DateIn instead of multiple dates in a vertical stack panel.

public class Item 
{         

    public string ID { get;set; }
    public IList<SubItem> SubItems { get; private set; }       
}
 public class SubItem
{
 public DateTime DateIn {get;set;}
 public DateTime DateOut {get;set;}   
}

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:xcdv="clr-namespace:Xceed.Wpf.DataGrid.Views;assembly=Xceed.Wpf.DataGrid"    
DataContext="{Binding ElementName=_this}"
>
<Window.Resources>         
    <xcdg:DataGridCollectionViewSource x:Key ="cvsList" Source="{Binding Items, ElementName=_this}" AutoCreateItemProperties="False">
        <xcdg:DataGridCollectionViewSource.ItemProperties>
        <xcdg:DataGridItemProperty Name="ID" Title="ID" DataType="{x:Type System:String}"/>
      <xcdg:DataGridItemProperty Name="DateIn" Title="Date In" ValuePath="SubItems" DataType="{x:Type System:String}" />
      <xcdg:DataGridItemProperty Name="DateOut" Title="Date Out" ValuePath="SubItems" DataType="{x:Type System:String}" />
    </xcdg:DataGridCollectionViewSource>
</Window.Resources>


    <xcdg:DataGridControl 
        Grid.Row="1" SelectionMode="Single"
        ItemsSource="{Binding Source={StaticResource cvsList}, NotifyOnTargetUpdated=True}" >                       

        <xcdg:DataGridControl.Columns>
           <xcdg:Column FieldName="ID" Title="ID" />
            <xcdg:Column FieldName="DateIn" Title="Date In" Width="150">
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=xcdg:DataRow},
                                                    Path=DataContext.SubItems}" >
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding DateIn, StringFormat=dd/MM/yyyy}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
            <xcdg:Column FieldName="DateOut" Title="Date Out" Width="150">
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=xcdg:DataRow},
                                                    Path=DataContext.SubItems}" >
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding DateOut, StringFormat=dd/MM/yyyy}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
        </xcdg:DataGridControl.Columns>                      
    </xcdg:DataGridControl>


Solution

  • I'm not familiar with Xceed but with the normal WPF datagrid, you could use converter classes that take an Item and convert them to multi line text (or a StackPanel if you really want that). So you would have one converter for the DateIn column and one for DateOut:

    <DataGrid AutoGenerateColumns="False" x:Name="dataGrid1" IsReadOnly="True" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}"/>
                <DataGridTextColumn Binding="{Binding ., Converter={StaticResource DateInConverter}, Mode=OneWay}"/>
                <DataGridTextColumn Binding="{Binding ., Converter={StaticResource DateOutConverter}, Mode=OneWay}"/>
            </DataGrid.Columns>
        </DataGrid>
    

    And the converter class definition could be:

    public class DateInConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var item = value as TestItem;
            if (item == null) return;
    
            var sb = new StringBuilder();
            item.SubItems.ForEach(x => sb.AppendLine(x.DateIn.ToShortDateString()));
    
            return sb.ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    If you need to learn about converters, see here.