Sometimes you may bind to some XmlDataProvider with the data provided as Xml Island. with XmlDataProvider, you may write something as follow.
<XmlDataProvider
x:Key="OrderData2"
XPath="Orders"
>
<x:XData>
<!--
caveat:
- see http://stackoverflow.com/questions/1899451/how-do-i-bind-to-xmldataprovider-whos-source-has-a-default-namespace-xmlns-set on how to bind XmlDataProvider with a default xmlns set
-->
<Orders>
<Order>
<ProductName>Hi-Speed Tonic</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>1</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>TNT</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>10</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>Jet-propelled Roller Skates</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>2</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>Portable Hole</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>15</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>Earthquake Pills</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>10</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>Rocket Sled</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>1</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
<Order>
<ProductName>Giant Rubber Band</ProductName>
<CostPerUnit>50.00</CostPerUnit>
<Quantity>2</Quantity>
<Discount>5</Discount>
<ShipAndHandle>10.00</ShipAndHandle>
</Order>
</Orders>
</x:XData>
</XmlDataProvider>
and you can do the following to bind to the XamDataGrid,
<ig:XamDataGrid
x:Name="xamDatagrid"
DataSource="{Binding Source={StaticResource OrderData2}, XPath=Order}" AutoFit="True"
>
<ig:XamDataGrid.FieldLayoutSettings>
<ig:FieldLayoutSettings AutoGenerateFields="True" />
</ig:XamDataGrid.FieldLayoutSettings>
</ig:XamDataGrid>
This all good when you are viewing from the designer, you can see that the Xml child element is autogenerated as a filed to the DataGrid.
But when you run, it shows nothing, why?
The reason of nothing display when you run
The reason is because the XML data island is embedded in the xaml file, and it uses the default xmlns, which is
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
but when you do XPath binding, it will translate to the default xmlns="". so there are two choices.
choice 1.
force the xml island to use the default empty xmlns, you can do this
<Orders xmlns="">
<Order>
...
</Order>
</Orders>
choice 2.
you can use the XmlNamespaceManager in the XmlDataProvider. like this,
<XmlDataProvider
x:Key="OrderData2"
XPath="/fb:Orders"
>
<XmlDataProvider.XmlNamespaceManager>
<XmlNamespaceMappingCollection>
<XmlNamespaceMapping
Uri="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Prefix="fb"
/>
</XmlNamespaceMappingCollection>
</XmlDataProvider.XmlNamespaceManager>
<x:XData>
<Orders>
<!-- the same as above -->
</Orders>
</XmlDataProvider>
and in the XamDataGrid, in the DataSource binding parts, do this:
<ig:XamDataGrid
x:Name="xamDatagrid"
DataSource="{Binding Source={StaticResource OrderData2}, XPath=fb:Order}" AutoFit="True"
> ...
<?ig:XamDataGrid>
you will notice the fb as in
XPath=fb:Order

本文介绍如何解决XML数据岛在WPF中绑定到XamDataGrid时遇到的问题,特别是当XML具有默认命名空间时。提供了两种解决方案:一是强制XML使用空命名空间;二是使用XmlNamespaceManager来指定命名空间。

被折叠的 条评论
为什么被折叠?



