我设法创建了以下内容:
如果我理解正确,在这种情况下,您必须使用嵌套的DataGrid
. 这个效果可以通过 来实现DataGridTemplateColumn.CellTemplate -> your DataTemplate
。此外,模式应该是 "normal" TextBlock
,因此它在嵌套DataGrid
隐藏时会显示一些值。将使用DataTrigger
in进行检查DataTemplate
。
XAML 代码:
<DataGrid Name="SimpleDataGrid" AutoGenerateColumns="False" RowHeaderWidth="0" CanUserAddRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" Loaded="SimpleDataGrid_Loaded">
<DataGrid.Columns>
<DataGridTemplateColumn Width="1.5*" Header="HeaderWithDataGrid" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<!-- Nested DataGrid -->
<DataGrid Name="InsertedDataGrid" AutoGenerateColumns="False" RowHeaderWidth="0" Loaded="InsertedDataGrid_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Header="InsertedHeader1" Width="1.5*" Binding="{Binding Name}" IsReadOnly="False" />
<DataGridTextColumn Header="InsertedHeader2" Width="1.5*" Binding="{Binding Age}" IsReadOnly="False" />
<DataGridTextColumn Header="InsertedHeader3" Width="1.5*" Binding="{Binding Name}" IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid>
<!-- Simply value, if nested DataGrid will be Hidden -->
<TextBlock Name="SimpleValue" Text="{Binding Age}" Visibility="Hidden" />
</Grid>
<DataTemplate.Triggers>
<!-- It checks for Hidden NestedDataGrid -->
<DataTrigger Binding="{Binding ShowInsertedGrid}" Value="Hidden">
<Setter TargetName="InsertedDataGrid" Property="Visibility" Value="Collapsed" />
<Setter TargetName="SimpleValue" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Simply column -->
<DataGridTextColumn Header="SimpleHeader" Width="1.5*" Binding="{Binding Name}" IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid>
Person类列表:
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
// For clarity using string.
// In real project using a bool.
public string ShowInsertedGrid
{
get;
set;
}
}
两个ObservableCollection
用于 DataGrid 的:
private ObservableCollection<Person> DataForDataGrid = new ObservableCollection<Person>();
private ObservableCollection<Person> DataForInsertedDataGrid = new ObservableCollection<Person>();
在Loaded
主 DataGrid 的处理程序事件集数据中:
private void SimpleDataGrid_Loaded(object sender, RoutedEventArgs e)
{
DataForDataGrid.Add(new Person()
{
Age = 2,
Name = "Nick",
ShowInsertedGrid = "Hidden", // Hidden NestedDataGrid
});
DataForDataGrid.Add(new Person()
{
Age = 1,
Name = "Sam",
});
DataForDataGrid.Add(new Person()
{
Name = "Kate",
Age = 15,
ShowInsertedGrid = "Hidden", // Hidden NestedDataGrid
});
SimpleDataGrid.ItemsSource = DataForDataGrid;
}
Loaded
NestedDataGrid 的事件处理程序:
private void InsertedDataGrid_Loaded(object sender, RoutedEventArgs e)
{
DataGrid MyDataGrid = sender as DataGrid;
DataForInsertedDataGrid.Add(new Person()
{
Name = "Bob",
Age = 15,
});
DataForInsertedDataGrid.Add(new Person()
{
Name = "SpanchBob",
Age = 151,
});
MyDataGrid.ItemsSource = DataForInsertedDataGrid;
}
为了美丽和改善外观,使用Styles
.
编辑:
如果您希望您的 NestedDataGrid
在整个Row上,那么我可以建议以下解决方案(不是最好的解决方案,但我只能提供这个):
对于每个单元格都将使用 DataTemplate。因此SimpleHeader
转化为:
<DataGridTemplateColumn Width="1.5*" Header="SimpleHeader" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<!-- Nested DataGrid -->
<DataGrid Name="InsertedDataGrid2" AutoGenerateColumns="False" RowHeaderWidth="0" Loaded="InsertedDataGrid2_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Header="InsertedHeader4" Width="1.5*" Binding="{Binding Name}" IsReadOnly="False" />
<DataGridTextColumn Header="InsertedHeader5" Width="1.5*" Binding="{Binding Age}" IsReadOnly="False" />
<DataGridTextColumn Header="InsertedHeader6" Width="1.5*" Binding="{Binding Name}" IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid>
<!-- Simply value, if nested DataGrid will be Hidden -->
<TextBlock Name="SimpleValue" Text="{Binding Name}" Visibility="Hidden" />
</Grid>
<DataTemplate.Triggers>
<!-- It checks for Hidden NestedDataGrid -->
<DataTrigger Binding="{Binding ShowInsertedGrid}" Value="Hidden">
<Setter TargetName="InsertedDataGrid2" Property="Visibility" Value="Collapsed" />
<Setter TargetName="SimpleValue" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
设置InsertedDataGrid2_Loaded
数据的位置:
private void InsertedDataGrid2_Loaded(object sender, RoutedEventArgs e)
{
DataGrid MyDataGrid = sender as DataGrid;
DataForInsertedDataGrid2.Add(new Person()
{
Name = "Bob2",
Age = 215,
});
DataForInsertedDataGrid2.Add(new Person()
{
Name = "SpanchBob2",
Age = 251,
});
MyDataGrid.ItemsSource = DataForInsertedDataGrid2;
}
对于使用 的每一列DataGrid
,以及为它加载数据的代码。因此,计算 main 和 nested 的总列数DataGrid
。