﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><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>语源科技BlogJava-微笑的子龙</title><link>http://www.blogjava.net/zxj8314/</link><description>for my dream</description><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 05:18:47 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 05:18:47 GMT</pubDate><ttl>60</ttl><item><title>Data Binding</title><link>http://www.blogjava.net/zxj8314/archive/2007/02/05/98100.html</link><dc:creator>zilong</dc:creator><author>zilong</author><pubDate>Mon, 05 Feb 2007 11:49:00 GMT</pubDate><guid>http://www.blogjava.net/zxj8314/archive/2007/02/05/98100.html</guid><wfw:comment>http://www.blogjava.net/zxj8314/comments/98100.html</wfw:comment><comments>http://www.blogjava.net/zxj8314/archive/2007/02/05/98100.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zxj8314/comments/commentRss/98100.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zxj8314/services/trackbacks/98100.html</trackback:ping><description><![CDATA[参考文章：<a href="http://www.beacosta.com/Archive/2005_09_01_bcosta_archive.html">http://www.beacosta.com/Archive/2005_09_01_bcosta_archive.html</a> <br />  1.绑定方式：<br />       1）Source/Data Context：<br />            <span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">&lt;Window.Resources&gt;<br />          &lt;local:GreekGod Name="Zeus" Description="Supreme God of    the    Olympians"       RomanName="Jupiter" x:Key="zeus"/&gt;<br />    &lt;local:GreekGod Name="Poseidon" Description="God of the sea, earthquakes and horses"    RomanName="Neptune" x:Key="poseidon"/&gt;<br />&lt;/Window.Resources&gt;<br /><br />&lt;StackPanel DataContext="{StaticResource poseidon}"&gt;<br />    &lt;TextBlock TextContent="{Binding Source={StaticResource zeus}, Path=Name}"/&gt;<br />    &lt;TextBlock TextContent="{Binding Path=Description}"/&gt;<br />    &lt;TextBlock TextContent="{Binding Path=RomanName}"/&gt;<br />&lt;/StackPanel&gt;<br /></font></span>Data Context允许元素从它的父元素继承数据绑定的数据源。<br /><span>Source</span><span>标记显式指定数据源。<br />一般情况下，绑定源对象中多个属性时用Data Context，绑定源对象中单个属性时用<span>Source。</span></span> 在绑定时 <span>Source</span>和Data Context效果一样，只是<span>Source的优先级比</span>Data Context 高。 如果绑定对象是一个xml，则{Binding}中属性用xpath来指定应该使用 XML 文档中的哪个集合来填充。   <br />2）ElementName：<br />3）<font face="Verdana">RelativeSource：<br />2.{binding}<br />   bingding有source和path属性，其中source属性指定绑定的具体数据对象，path指定该对象的特定属性。当逻辑树中有Data Context，可以不用设定binding中source属性；当binding中没有设定path属性表明绑定整个对象。<br /><span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">&lt;Window.Resources&gt;<br />    &lt;local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/&gt;<br />&lt;/Window.Resources&gt;<br /><br />&lt;Border DataContext="{StaticResource zeus}"&gt;<br />    &lt;ContentControl Content="{Binding}"/&gt;<br />&lt;/Border&gt;</font><br /></span><br />   如果要将某一元素与对象的多个属性绑定时，</font><font face="Courier" color="#6666cc" size="2">ContentControl 不知道如何GreekGod 的信息。这时要用到<font face="Verdana" color="#000000">DataTemplate，它的作用是指定数据显示形式。<br />   <span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">&lt;Window.Resources&gt;<br />    &lt;local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/&gt;<br /><br />    &lt;DataTemplate x:Key="contentTemplate"&gt;<br />        &lt;DockPanel&gt;<br />            &lt;TextBlock Foreground="RoyalBlue" TextContent="{Binding Path=Name}" /&gt;<br />            &lt;TextBlock TextContent=":" Margin="0,0,5,0" /&gt;<br />            &lt;TextBlock Foreground="Silver" TextContent="{Binding Path=Description}" /&gt;<br />        &lt;/DockPanel&gt;<br />    &lt;/DataTemplate&gt;<br />&lt;/Window.Resources&gt;<br /><br />&lt;Border DataContext="{StaticResource zeus}"&gt;<br />    &lt;ContentControl Content="{Binding}" ContentTemplate="{StaticResource contentTemplate}"/&gt;<br />&lt;/Border&gt;</font><br /></span><br />注意：<font face="Courier" color="#6666cc">DataTemplate中的{binding}没有source属性，这是因为自动地将Data Context设为数据对象绑定方式。 </font><br />3、<font face="Courier" color="#6666cc">get ListItem from a data bound Listbox<br /></font>xaml:<br />    <span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">&lt;Window.Resources&gt;<br />    &lt;local:GreekGods x:Key="greekGods"/&gt;<br /><br />    &lt;DataTemplate x:Key="itemTemplate"&gt;<br />        &lt;TextBlock Text="{Binding Path=Name}" /&gt;<br />    &lt;/DataTemplate&gt;<br />&lt;/Window.Resources&gt;<br /><br />&lt;ListBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Name="listBox"/&gt;<br /></font><font color="#000000"><br />listBox的ItemSource有一个<font face="Verdana">IEnumerable接口，是你想要显示的items列表</font> Itemtemplate属性指定用来控制数据显示的datatemplate。<br /></font>c#:<br /><span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">GreekGod greekGod = (GreekGod)(listBox.Items[0]);//绑定的数据对象；<br /><span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));//返回ListBoxItem；</font><br /><span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));<br />为了保证选中项与当前项同步，设定</font><font face="Verdana" color="#000000"><font size="2"> IsSynchronizedWithCurrentItem= "true".<br /><br />4.get a ComboBoxItem from a data bound ComboBox<br />与listbox相似；<br /></font><font size="2"><font face="Courier" color="#6666cc">Window.Resources&gt;<br />    &lt;local:GreekGods x:Key="greekGods"/&gt;<br /><br />    &lt;DataTemplate x:Key="itemTemplate"&gt;<br />        &lt;TextBlock Text="{Binding Path=Name}" /&gt;<br />    &lt;/DataTemplate&gt;<br />&lt;/Window.Resources&gt;<br /><br />&lt;ComboBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Width="200" Name="comboBox"/&gt;</font><br /><br />c#:<br /></font><span style="FONT-SIZE: 85%; COLOR: #6666cc; FONT-FAMILY: courier"><font size="2">GreekGod greekGod = (GreekGod)(comboBox.Items[0]);<br />comboBox.IsDropDownOpen = true;<br />ComboBoxItem cbi1 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromIndex(0));<br />ComboBoxItem cbi2 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.Items.CurrentItem));<br />comboBox.IsDropDownOpen = false;<br />注意：<br />调用<font face="Verdana" color="#000000">ContainerFromIndex之前要先打开组合框。<font face="Courier" color="#6666cc">comboBox.IsDropDownOpen = true;</font><br /></font></font></span></font></span></span></font></span></span></font></font><img src ="http://www.blogjava.net/zxj8314/aggbug/98100.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zxj8314/" target="_blank">zilong</a> 2007-02-05 19:49 <a href="http://www.blogjava.net/zxj8314/archive/2007/02/05/98100.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>