我需要在DataGrid
ItemsCollectionA , ItemsCollectionB 和 ItemsCollectionC 都是ObservableCollections
。
我有一个datagrid:
<DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" />
以网格格式显示所有属性,但 ItemsCollectionB 和 ItemsCollectionC 显示为(集合)。
如何获取 ItemsCollectionB 和 ItemsCollectionC 以向下展开网格并向外展示其属性
答案 0 :(得分:3)
如果您可以指定列而不是自动生成列,这可能非常简单。
以下是一个例子:
<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;
}
}
将ItemsCollectionA
视为Employees
,将ItemsCollectionB
和ItemsCollectionC
视为Dogs
和Cats
。它仍然使用ToString
来显示我覆盖的Dogs
和Cats
对象,但您只需在列中的列表框中设置DataTemplate
即可确定如何显示你的模特。另请注意AutoGenerateColumns="False"
上的DataGrid
,以避免两次创建列。
希望这有帮助
答案 1 :(得分:2)
DataGrid 只能管理一个项目来源。一切都是基于行的,列不是那么聪明。
两种方式:
答案 2 :(得分:1)
好吧,似乎datagrid完全是我在这里需要的错误。我创建了一个listbox的stackpanel,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>