`

wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island

阅读更多

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

 

 

 

分享到:
评论

相关推荐

    WPF XmlDataProvider 读取xml数据,支持添加,删除,list列表

    在Windows Presentation Foundation (WPF) 中,`XmlDataProvider` 是一个强大的工具,它允许开发者将XML数据绑定到用户界面元素,实现数据驱动的界面。这个技术对于构建动态、响应式的应用程序至关重要,因为它使得...

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架.zip

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 4

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-Samples-master_WPF基本sample_

    WPF的基本空间历程,使用.net core3.0.1版本

    WPF-3-Building Web Browser Applications with Windows Presentation Foundation

    "WPF-3-Building Web Browser Applications with Windows Presentation Foundation" 这个主题专注于如何利用WPF来构建功能丰富的Web浏览器应用程序。 在WPF中,开发人员可以利用XAML(Extensible Application ...

    WPF-Diagram-Designer:WPF图表设计器源代码

    通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...

    HMI-with-WPF-part-3-Communication-with-multiple-plc-master

    标题 "HMI-with-WPF-part-3-Communication-with-multiple-plc-master" 指的是一项关于使用Windows Presentation Foundation(WPF)开发人机界面(HMI)并与多个西门子PLC进行通信的项目。这个项目的重点在于通过...

    wpf---StatusBar

    “wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...

    WPF-强大的图表.zip

    **WPF - 强大的图表技术** Windows Presentation Foundation (WPF) 是Microsoft开发的一个用于构建桌面应用程序的框架,它提供了丰富的图形系统,包括对2D和3D图形的强大支持。在WPF中,开发人员可以利用各种图表...

    wpf-datagrid-access DB

    在这个“wpf-datagrid-access DB”主题中,我们将深入探讨如何利用WPF Datagrid与Microsoft Access数据库进行交互,实现数据的读取、更新和保存。 1. **WPF Datagrid简介** - Datagrid是WPF中的一个数据展示控件,...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手1

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-MVVM-简单示例

    **WPF-MVVM-简单示例** 在本文中,我们将深入探讨如何使用MVVM(Model-View-ViewModel)设计模式来构建一个基于WPF(Windows Presentation Foundation)的应用程序。这个示例着重于通过操作XML数据来实现基本的CRUD...

    WPF-ScreenData.zip

    超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...

    wpf-wcf-wf

    **WPF-WCF-WF整合开发实例** WPF(Windows Presentation Foundation)、WCF(Windows Communication Foundation)和WF(Windows Workflow Foundation)是微软.NET框架中三个关键的技术组件,它们各自负责不同的领域...

    WPF-XamlPad程序

    在本篇文章中,我们将深入探讨WPF-XamlPad程序,以及与之相关的VisualTree和LogicalTree概念,还有Template的使用。 首先,让我们了解**XamlPad**。XamlPad是一个简单的编辑器,允许开发者输入纯XAML代码并即时预览...

    WPF--Effect特效

    标题中的"WPF--Effect特效"指的是Windows Presentation Foundation(WPF)中的Effect特效。WPF是.NET Framework的一个重要组件,用于构建丰富的桌面应用程序。Effect特效在WPF中是用来改变UI元素外观的一...

    WPF-Ribbon

    WPF-Ribbon则是专门针对WPF平台实现的Ribbon组件,它为开发者提供了在WPF应用程序中集成这种现代用户界面的能力。 1. **Ribbon布局** - Ribbon界面通常包含多个主要区域:快速访问工具栏(QAT)、主选项卡、上下文...

    gong-wpf-dragdrop-develop.rar

    gong-wpf-dragdrop-develop.rar

    File-Manager-UI-Wpf-master.7z

    【标题】"File-Manager-UI-Wpf-master.7z" 涉及的是一个基于WPF(Windows Presentation Foundation)的文件管理器用户界面项目。这个压缩包包含了一个完整的开发环境,供开发者进行学习和使用。 【描述】"自用资源...

Global site tag (gtag.js) - Google Analytics