WPF绑定各种数据源之xml数据源

时间:2022-01-20 08:56:16

一、WPF绑定各种数据源索引

WPF 绑定各种数据源之Datatable

WPF绑定各种数据源之object数据源

WPF绑定各种数据源之xml数据源

WPF绑定各种数据源之元素控件属性

Binding的基础可参考WPF 绑定基础

二、WPF绑定各种数据源之xml数据源,此时的XML源写在界面了,当然也可以独立成文件。

<Window.Resources>

       <Con:BackgroundConverter x:Key="BackgroundConverter"/>

       <XmlDataProvider x:Key="myPerson3">

           <x:XData>

               <PersonF xmlns="">

                   <person Name="Person1">

                       <ID>1</ID>

                       <Name>XiaoA</Name>

                       <Age>49</Age>

                   </person>

                   <person Name="Person2">

                       <ID>2</ID>

                       <Name>XiaoB</Name>

                       <Age>29</Age>

                   </person>

                   <person Name="Person3">

                       <ID>3</ID>

                       <Name>XiaoC</Name>

                       <Age>103</Age>

                   </person>

                   <person Name="Person4">

                       <ID>4</ID>

                       <Name>XiaoD</Name>

                       <Age>59</Age>

                   </person>

               </PersonF>

           </x:XData>

       </XmlDataProvider>

   </Window.Resources>

 

  下面是绑定的代码。此时需要注意,原来用Path改成了XPath,因为这是XML源,并且ItemsSource 改成 ItemsSource="{Binding Source={StaticResource myPerson3},XPath=http://www.mamicode.com/PersonF/person}"

<ListView Height="262" Margin="0,32,56,0" ItemsSource="{Binding Source={StaticResource myPerson3},XPath=http://www.mamicode.com/PersonF/person}" VerticalAlignment="Top"  Name="listView3" HorizontalAlignment="Right" Width="310">

      <ListView.View>

          <GridView>