如何将elemnte xml保存到wpf?

时间:2013-02-03 10:25:36

标签: wpf c#-4.0 wpf-controls

我们有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>

绑定到datagrid:

   <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>

输出是这样的:

enter image description here

我们希望此数据网格具有保存和删除功能,例如:

<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>

0 个答案:

没有答案