3

我需要在一个对象中显示一个对象的所有信息DataGrid

ItemsCollectionAItemsCollectionBItemsCollectionC都是ObservableCollections.

我有一个数据网格:

<DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" />

这会以网格格式显示所有属性,但ItemsCollectionBItemsCollectionC显示为(集合)。

在此处输入图像描述

如何让ItemsCollectionBItemsCollectionC向下和向外扩展网格以显示它们的属性

4

3 回答 3

4

如果您可以指定列而不是自动生成它们,这可能非常容易。

这是一个例子:

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding EmployeeName}"/>
        <!-- Displays the items of the first collection-->
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Dogs}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <!-- Displays the items of the second collection-->
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Cats}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

视图模型:

public class MainWindowViewModel : NotificationObject
{
    public MainWindowViewModel()
    {
        Employees = new ObservableCollection<Employee>
        {
            new Employee { EmployeeName = "Steven"},
            new Employee { EmployeeName = "Josh"},
        };
    }

    public ObservableCollection<Employee> Employees { get; set; }
}

楷模:

public class Employee
{
    public Employee()
    {
        Dogs = new ObservableCollection<Dog>
        {
            new Dog { Gender = 'M'},
            new Dog { Gender = 'F'},
        };
        Cats = new ObservableCollection<Cat>
        {
            new Cat { Name = "Mitzy" , Kind = "Street Cat"},
            new Cat { Name = "Mitzy" , Kind = "House Cat"}
        };
    }

    public string EmployeeName { get; set; }

    public ObservableCollection<Dog> Dogs { get; set; }

    public ObservableCollection<Cat> Cats { get; set; }
}

public class Dog
{
    public char Gender { get; set; }

    public override string ToString()
    {
        return "Dog is a '" + Gender + "'";
    }
}

public class Cat
{
    public string Name { get; set; }

    public string Kind { get; set; }

    public override string ToString()
    {
        return "Cat name is " + Name + " and it is a " + Kind;
    }
}

考虑ItemsCollectionAasEmployees和as和。ItemsCollectionB_ 它仍然使用来显示我覆盖的对象,但是您可以简单地将 a设置为列中的列表框来决定如何显示您的模型。还要注意on以避免两次创建列。ItemsCollectionCDogsCatsToStringDogsCatsDataTemplateAutoGenerateColumns="False"DataGrid

希望这可以帮助

于 2013-11-04T13:44:13.357 回答
2

一个DataGrid只能管理一个项目源。一切都是基于行的,列并不那么智能。

两种方式:

  • 将您的数据组合成具有两组字段的新对象(将是最简单的)。
  • 并排同步 2 个数据网格。
于 2013-11-04T13:25:32.003 回答
1

好吧,似乎数据网格完全是我在这里需要的错误。我制作了一个列表框的堆栈面板,其中 itemssource 设置为每个绑定,并且显示正常

<StackPanel Background="white" HorizontalAlignment="Stretch"  Margin="0">
        <ListBox Background="white" x:Name="BetsListBox"  VerticalAlignment="Stretch" BorderThickness="0" 
                     ItemsSource="{Binding Path=ItemsCollectionA}" Margin="0" Width="Auto" HorizontalAlignment="Stretch" >
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F0F0F0"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#F0F0F0"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <ListBox ItemsSource="{Binding Path=ItemsCollectionB}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlockText="{Binding Path=varA}" />
                                        <TextBlockText="{Binding Path=varB}" />
                                        <TextBlockText="{Binding Path=varC}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        <ListBox BorderThickness="0" ItemsSource="{Binding Path=ItemsCollectionC}" >
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel  Orientation="Horizontal" >
                                        <TextBlock Text="{Binding Path=VarA}" ToolTip="{Binding Name}"/>
                                        <TextBlock Text="{Binding Path=VarB}" ToolTip="{Binding Name}"/>
                                        <TextBlock Text="{Binding Path=VarC}" ToolTip="{Binding Name}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox >
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
于 2013-11-06T13:36:51.573 回答