アイテムのリストがあり、各アイテムにはサブアイテムのリストが含まれています。これらをアイテムごとに1行のXceedデータグリッドに表示しようとしていますが、複数のサブアイテムがある場合は、同じ行の対応する列の垂直スタックパネルにこれらを追加します。データグリッドは次のようになります(ダッシュは無視してください。テキストの配置に使用しました)。
---------- ID ------- DateIn---------DateOut
行1-ID1----2012年10月2日-2012年11月2日
行2--ID2----2012年10月3日-2012年11月3日
------------------11/03/2012--12/03/2012
------------------12/03/2012--13/03/2012
行3-ID3----2012年11月3日-2012年12月3日
現在のコードは以下のとおりです。垂直スタックパネルに複数の日付ではなく、最初のサブアイテムDateInのみが表示されます。
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>