WPF绑定XML ListBox显示

Window1.xaml  前台代码

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPFDataBindingXML" Height="372" Width="462">
   
    <Window.Resources>
        <XmlDataProvider x:Key="guestSource" Source="guest.xml" XPath="/Guests/*"/>
        <DataTemplate x:Key="showTitle">
            <TextBlock Text="{Binding XPath=Title}"/>
        </DataTemplate>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="FontFamily" Value="Trebuchet MS"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
    </Window.Resources>
   
    <Grid>
       
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
       
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
       
        <ListBox Grid.Row="0" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Source={StaticResource guestSource}}"
                 ItemTemplate="{StaticResource showTitle}" IsSynchronizedWithCurrentItem="True">
        </ListBox>
       
        <StackPanel Grid.Row="0" Grid.Column="1" Margin="5,5,5,5">
            <TextBlock>
                    Title:
                <TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Title}"/>
            </TextBlock>
            <TextBlock>
                    Coment:
                <TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Coment}"/>
            </TextBlock>
            <TextBlock>
                    Reply:
                <TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Reply}"/>
            </TextBlock>
        </StackPanel>
       
    </Grid>
</Window>

******************************************************

guest.xml 数据结构

<?xml version="1.0" encoding="utf-8" ?>
<Guests>
  <xs:schema id="Guests" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Guests" msdata:IsDataSet="true">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Guest">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Title" type="xs:string" minOccurs="0" />
                <xs:element name="Coment" type="xs:string" minOccurs="0" />
                <xs:element name="Reply" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Guest>
    <Title>sohu</Title>
    <Coment>job plan</Coment>
    <Reply>嘿嘿呵呵</Reply>
  </Guest>
  <Guest>
    <Title>why?</Title>
    <Coment>because ?</Coment>
    <Reply>嘿嘿?</Reply>
  </Guest>
  <Guest>
    <Title>fet</Title>
    <Coment>job plan</Coment>
    <Reply>嘿嘿呵呵</Reply>
  </Guest>
  <Guest>
    <Title>goog</Title>
    <Coment>job plan</Coment>
    <Reply>嘿嘿呵呵</Reply>
  </Guest>
  <Guest>
    <Title>heal</Title>
    <Coment>job plan</Coment>
    <Reply>嘿嘿呵呵</Reply>
  </Guest>
</Guests>

转载于:https://www.cnblogs.com/GT_Andy/archive/2009/10/13/1921972.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值