Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定之二:使用外部URL的XML文件)

116 篇文章 9 订阅
27 篇文章 0 订阅

--------------------------------------------------------------------------------
引用或转载时请保留以下信息:
大可山 [MSN:a3news(AT)hotmail.com]
http://www.zpxp.com http://www.brawdraw.com
萝卜鼠在线图形图像处理
--------------------------------------------------------------------------------

3、使用外部URL链接的XML文件作为数据源
事实上,在上文Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) 中,XmlDataProvider的Source属性还可以直接指向你本机的某个XML文件:
<XmlDataProvider x:Key="FavoriteColors" Source="E:/Books/xaml/FavoriteColors.xml" />

我们还可以指向任何标准 URL,这样,我们就可以创建对XML API(如 RSS)的快速访问。
比如,我的BLOG中关于WPF的RSS地址是:http://blog.csdn.net/johnsuna/category/325343.aspx/rss,下面我使用TextBlock来显示BLOG的标题,用ListBox来显示所有文章的题目。

<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="WPFDataBinding.BindingRss"
 x:Name="BindingRss1"
 Title="Window1"
 >
<Canvas x:Name="LayoutRoot">
<StackPanel Height="243.687">
  <StackPanel.Resources>
   <XmlDataProvider x:Key="MyRSS"
    Source="
http://blog.csdn.net/johnsuna/category/325343.aspx/rss" />
 </StackPanel.Resources>

 <TextBlock x:Name="txtBlockTitle" Text="{Binding Source={StaticResource MyRSS}, XPath=//channel/title}" HorizontalAlignment="Center"
               FontWeight="Bold"/>
 <ListBox Width="500" Height="300"
             ItemsSource="{Binding Source={StaticResource MyRSS}, XPath=//item/title}">
 </ListBox>
 </StackPanel>
</Canvas>
</Window> 

上面 XPath=//channel/title , XPath=//item/title分别对应下面的具体内容,你可以结合起来仔细分析一下。(如果你对XPath的相关细节不够了解,建议你看一下XSLT相关的书或者到以下链接:http://www.w3.org/TR/xpathhttp://www.w3schools.com/xpath/default.asp

这是http://blog.csdn.net/johnsuna/category/325343.aspx/rss的部分内容:(注意我特别加粗的部分)
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
  <title>Johnson(大可山)的专栏 -</title>

  <link>http://blog.csdn.net/johnsuna/category/325343.aspx</link>
  <description />
  <dc:language>af</dc:language>
  <generator>.Text Version 1.0.1.1</generator>
  <image>http://counter.csdn.net/pv.aspx?id=72</image>
 <item>
  <dc:creator>大可山</dc:creator>
  <title>Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)</title>
  <link>http://blog.csdn.net/johnsuna/archive/2007/08/21/1753001.aspx</link>
  <pubDate>Tue, 21 Aug 2007 15:59:00 GMT</pubDate>
  <guid>http://blog.csdn.net/johnsuna/archive/2007/08/21/1753001.aspx</guid>
  <wfw:comment>http://blog.csdn.net/johnsuna/comments/1753001.aspx</wfw:comment>
  <comments>http://blog.csdn.net/johnsuna/archive/2007/08/21/1753001.aspx#Feedback</comments>
  <slash:comments>0</slash:comments>
  <wfw:commentRss>http://blog.csdn.net/johnsuna/comments/commentRss/1753001.aspx</wfw:commentRss>
  <trackback:ping>http://tb.blog.csdn.net/TrackBack.aspx?PostId=1753001</trackback:ping>
  <description><img src ="http://blog.csdn.net/johnsuna/aggbug/1753001.aspx" width = "1" height = "1" /></description>
 </item>
 <item>
  <dc:creator>大可山</dc:creator>
  <title>Windows Presentation Foundation(WPF)中的数据绑定(控件与控件值的绑定)</title>
  <link>http://blog.csdn.net/johnsuna/archive/2007/08/21/1752865.aspx</link>
  <pubDate>Tue, 21 Aug 2007 14:51:00 GMT</pubDate>
  <guid>http://blog.csdn.net/johnsuna/archive/2007/08/21/1752865.aspx</guid>
  <wfw:comment>http://blog.csdn.net/johnsuna/comments/1752865.aspx</wfw:comment>
  <comments>http://blog.csdn.net/johnsuna/archive/2007/08/21/1752865.aspx#Feedback</comments>
  <slash:comments>0</slash:comments>
  <wfw:commentRss>http://blog.csdn.net/johnsuna/comments/commentRss/1752865.aspx</wfw:commentRss>
  <trackback:ping>http://tb.blog.csdn.net/TrackBack.aspx?PostId=1752865</trackback:ping>
  <description>使用类似{Binding ElementName=txtBoxInput,Path=Text}的方式进行控件与控件的绑定<img src ="http://blog.csdn.net/johnsuna/aggbug/1752865.aspx" width = "1" height = "1" /></description>
 </item>
<!-- 还有更多 -->
</channel>
</rss>

下图是显示效果:
使用RSS等外部文件进行WPF控件绑定

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值