我有一個項目列表和每個項目包含子項的列表。我試圖在一個Xceed數據網格中顯示這些數據,每個項目只有一行,但是如果有多個子項目,我想在同一行的垂直堆棧面板中的相應列中添加這些項目。數據網格應該是這樣的(忽略破折號,我用它們來對齊文本):DataGrid行與多個項目
---------- ID ------- ------ DateIn --- DateOut
行1 --ID1 ---- 10/02/2012 --11/02/2012
行2 --ID2 ---- 10/03/2012 --11/03/2012
------------------ 11/03/2012 - 2012/12/12
---------- -------- 10/03/2012--13/03/2012
行3 --ID3 ---- 2012年11月3日 - 12/03/201 2
當前代碼在下面,它只顯示第一個子項目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>
感謝您的回答。現在需求已經改變,所以我不需要使用它,但我會將其標記爲答案 – 2012-04-23 03:00:06