2012-04-21 56 views
1

我有一個項目列表和每個項目包含子項的列表。我試圖在一個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> 

回答

1

我不熟悉Xceed但與正常的WPF的DataGrid,您可以使用,採取項目轉換器類,並將其轉換爲多行文本(或一個StackPanel,如果你真的想要的)。所以,你將不得不爲DateIn列一個轉換器,一個用於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> 

和轉換器類的定義可能是:

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

如果您需要了解轉換器,看here

+0

感謝您的回答。現在需求已經改變,所以我不需要使用它,但我會將其標記爲答案 – 2012-04-23 03:00:06