Datagrid行包含多个项目

时间:2012-04-21 06:42:26

标签: wpf datagrid itemscontrol

我有一个项目列表,每个项目都包含一个子项目列表。我试图在Xceed数据网格中显示这些项目,每个项目一行,但如果有多个子项目,我想在同一行的垂直堆栈面板中添加这些子项目,在相应的列中。数据网格应如下所示(忽略破折号,我用它们来对齐文本):

---------- ID ------- DateIn --------- DateOut
第1行--ID1 ---- 10/02/2012 - 11/02/2012

第2行--ID2 ---- 10/03/2012 - 11/03/2012

------------------ 11/03/2012 - 2012年3月3日

------------------ 10/03 / 2012--13 / 03/2012

第3行--ID3 ---- 11/03/2012 - 2012年3月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>

1 个答案:

答案 0 :(得分:1)

我不熟悉Xceed,但是对于普通的WPF数据网格,你可以使用转换器类来获取一个Item并将它们转换为多行文本(如果你真的想要那么就转换为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