wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island

46 篇文章 0 订阅

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值