1

我们有 xml 列:

<Department Name='D1' TotalCapacity='5'>
  <Class Name='c1' Capacity='3'></Class>
  <Class Name='c2' Capacity='2'></Class>
</Department>

<Department Name='D2' TotalCapacity='10'>
  <Class Name='c1' Capacity='5'></Class>
  <Class Name='c3' Capacity='5'></Class>
</Department>

绑定到数据网格:

   <Grid>
    <Grid.Resources>
        <XmlDataProvider x:Key="DepartmentData" XPath="Departments/Department">
            <x:XData>
                <Departments xmlns="">
                    <Department Name='D1' TotalCapacity='5'>
                        <Class Name='c1' Capacity='3'/>
                        <Class Name='c2' Capacity='2'/>
                    </Department>
                    <Department Name='D2' TotalCapacity='10'>
                        <Class Name='c1' Capacity='5'/>
                        <Class Name='c3' Capacity='5'/>
                    </Department>
                </Departments>
            </x:XData>
        </XmlDataProvider>
    </Grid.Resources>
    <DataGrid ItemsSource="{Binding Source={StaticResource DepartmentData}}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Department Name" Binding="{Binding XPath=@Name}"/>
            <DataGridTextColumn Header="Total Capacity" Binding="{Binding XPath=@TotalCapacity}"/>
            <DataGridTextColumn Header="c1">
                <DataGridTextColumn.Binding>
                    <Binding XPath="Class[@Name='c1']/@Capacity"/>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
            <DataGridTextColumn Header="c2">
                <DataGridTextColumn.Binding>
                    <Binding XPath="Class[@Name='c2']/@Capacity"/>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
            <DataGridTextColumn Header="c3">
                <DataGridTextColumn.Binding>
                    <Binding XPath="Class[@Name='c3']/@Capacity"/>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

输出是这样的:

在此处输入图像描述

我们希望这个数据网格有保存和删除,例如:

<Department Name='D1' TotalCapacity='5'>
  <Class Name='c1' Capacity='3'></Class>
  <Class Name='c2' Capacity='2'></Class>
</Department>

删除

<Department Name='D1' TotalCapacity='5'>
  <Class Name='c1' Capacity='3'></Class>
</Department>

和:

<Department Name='D1' TotalCapacity='5'>
  <Class Name='c1' Capacity='3'></Class>
  <Class Name='c2' Capacity='2'></Class>
</Department>

添加

<Department Name='D1' TotalCapacity='5'>
  <Class Name='c1' Capacity='3'></Class>
  <Class Name='c2' Capacity='2'></Class>
  <Class Name='c5' Capacity='8'></Class>
</Department>
4

0 回答 0