方式一、
效果图:
前台代码:
<dxg:GridControl Name="grid1" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True">
<dxg:GridControl.View>
<dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="id" Width="*" />
<dxg:GridColumn FieldName="name" Width="*" />
</dxg:GridControl.Columns>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor Name="gridDetailDescriptor" ItemsSourceBinding="{Binding Path=list_order}" ShowHeader="false">
<dxg:GridControl Name="detailGrid">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="index" Width="*" />
<dxg:GridColumn FieldName="id2" Width="*" />
<dxg:GridColumn FieldName="name2" Width="*" />
</dxg:GridControl.Columns>
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem SummaryType="Count" FieldName="name2" />
</dxg:GridControl.TotalSummary>
<dxg:GridControl.View>
<dxg:TableView Name="detailView"
DetailHeaderContent="Orders"
UseAnimationWhenExpanding="true"
ShowColumnHeaders="True"
AllowFixedColumnMenu="true"
ShowTotalSummary="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
方法二 模板方式前台:
<Window.Resources>
<ResourceDictionary>
<!--#region contentTemplate-->
<DataTemplate x:Key="contentTemplate">
<dx:MeasurePixelSnapper Margin="5,5,10,10">
<dx:DXTabControl Margin="0" >
<dx:DXTabItem Header="Orders">
<dxg:GridControl Name="detailGrid" ItemsSource="{Binding list_order}" MaxHeight="200" >
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="index" Width="*" />
<dxg:GridColumn FieldName="id2" Width="*" />
<dxg:GridColumn FieldName="name2" Width="*" />
</dxg:GridControl.Columns>
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem SummaryType="Count" FieldName="name2" />
</dxg:GridControl.TotalSummary>
<dxg:GridControl.View>
<dxg:TableView VerticalScrollbarVisibility="Auto" NavigationStyle="Row" ShowGroupPanel="False" />
</dxg:GridControl.View>
</dxg:GridControl>
</dx:DXTabItem>
<dx:DXTabItem Header="Customers">
</dx:DXTabItem>
</dx:DXTabControl>
</dx:MeasurePixelSnapper>
</DataTemplate>
<!--#endregion-->
</ResourceDictionary>
</Window.Resources>
<Grid>
<dxg:GridControl Name="grid1" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True">
<dxg:GridControl.View>
<dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="id" Width="*" />
<dxg:GridColumn FieldName="name" Width="*" />
</dxg:GridControl.Columns>
<dxg:GridControl.DetailDescriptor>
<dxg:ContentDetailDescriptor ShowHeader="True"
ContentTemplate="{StaticResource contentTemplate}" />
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
</Grid>
后台统一代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
namespace IMOS.DEVMain
{
/// <summary>
/// Interaction logic for test2.xaml
/// </summary>
public partial class test2 : ThemedWindow
{
public test2()
{
InitializeComponent();
List<TestModel> list = new List<TestModel>();
for (int i = 0; i < 10; i++)
{
List<Orders> list2 = new List<Orders>();
for (int j = 0; j < 5; j++)
{
Orders model2 = new Orders();
model2.index = j + 1;
model2.id2 = i + "-" + j;
model2.name2 = "我是二级详情:" + j;
list2.Add(model2);
}
TestModel model = new TestModel();
model.id = i;
model.name = "我的主数据:" + i;
model.list_order = list2;
list.Add(model);
}
grid1.ItemsSource = list;
}
}
public class TestModel
{
public int id { set; get; }
public string name { set; get; }
public List<Orders> list_order { set; get; }
}
public class Orders
{
public int index { set; get; }
public string id2 { set; get; }
public string name2 { set; get; }
}
}