惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - kings

项目团队成长日志--聆听客户声音,沟通无所不在 TOAD使用筆記 使电脑鼠标右键相应快的办法 非常好用的对日面试资料(转) IT技術者日本語面接(ぎじゅつしゃにほんごめんせつ)によく出(で)る100質問(しつもん) 获取应用程序路径 再议 构造方法(转自Q.yuhen) 基元类型、值类型和引用类型(转自Q.yuhen) C# 2.0 - 泛型(Generics)(转自Q.yuhen) new 和 override 的区别(转自Q.yuhen) “多态”一个需要注意的问题(转自Q.yuhen) C# 方法参数 ref 详述(转自Q.yuhen) 浅析Family Show 2.0的动态换肤实现(转tonyqus) 浅析Family Show 2.0的子窗体实现(转tonyqus) webservice 遇到的小问题 泛型集合类型,赋予集合业务意义,增强集合的抽象使用(转-lizhe1985) WPF_Markup的几种写法 When I tab into a toolbar in WPF I can't tab out again? What can I do to change this tab behaviour? Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定之二:使用外部URL的XML文件)(转-大可山)
Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)(转-大可山)
kings · 2007-09-01 · via 博客园 - kings

上篇:Windows Presentation Foundation(WPF)中的数据绑定(控件与控件值的绑定) 一文中讲了使用{Binding ElementName=xxx,Path=xxx}的形式实现了控件到控件的绑定,注意它使用了ElementName属性。本篇将说说如何使用XmlDataProvider作控件绑定,但它使用Source 属性。

对于大多数应用程序,我们希望绑定到XML或 .NET 对象。比如XML文件或者自定义的.Net对象,或者是我们从数据库中取出的相应记录。

XAML 用其 Provider 对象支持该操作。XAML 中内置有两种类型的数据提供程序:ObjectDataProvider 和 XMLDataProvider。其中,ObjectDataProvider 用于绑定到 .NET 对象以及从 .NET 对象绑定;XMLDataProvider 用于绑定到 XML 片段和文档以及从 XML 片段和文档进行绑定。

2. XMAL文件内部数据的直接绑定:
首先我们来看看直接在XAML中如何使用XMLDataProvider进行内部数据的直接绑定。
<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="WPFDataBinding.Window1"
 x:Name="Window"
 Title="Window1"
 >
<Canvas x:Name="LayoutRoot">
<StackPanel Width="214" Height="Auto">
   <StackPanel.Resources>
     <XmlDataProvider x:Key="FavoriteColors">
       <x:XData>
         <Colors xmlns="">
           <Color>红</Color>
           <Color>橙</Color>
           <Color>黄</Color>
           <Color>绿</Color> 
           <Color>蓝</Color>
           <Color>靛</Color>
           <Color>紫</Color>

         </Colors>
       </x:XData>
    </XmlDataProvider>

   </StackPanel.Resources>
   <TextBlock HorizontalAlignment="Center" FontWeight="Bold">
      喜欢的颜色
   </TextBlock>
   <ListBox Width="200" Height="120"
               ItemsSource="{Binding Source={StaticResource FavoriteColors}, XPath=/Colors/Color}">
   </ListBox>
</StackPanel>
</Canvas>
</Window>

可以看到,在 StackPanel 的资源(<StackPanel.Resources> )中,有一个 XMLDataProvider 对象。x:Key 表示 Binding 对象中用来引用它的名称。这里,我们创建了 XML 内联数据(数据使用<x:XData>标签包含起来,关于XData的细节,建议你查查MSDN:ms-help://MS.LHSMSSDK.1033/MS.LHSNETFX30SDK.1033/wpf_conceptual/html/7ce209c2-621b-4977-b643-565f7e663534.htm,有更详细的说明),用作数据绑定的源。在 ListBox 的 Binding 中,使用FavoriteColors的StaticResource作为Source。如果某个数据源位于 XAML 文档中,我们就要指定该对象是一个静态源(StaticResource),正如这里所看到的一样。最后,我们使用 XPath 语句指定应该使用 XML 文档中的哪个集合来填充 ListBox。

最后的结果:
XAML内联数据绑定到控件