<?xml version="1.0" encoding="utf-8" ?><rss version="2.0"><channel><title>DongPad</title><link>http://www.dongpad.com</link> <description>Every day is a new beginning!</description><copyright>2.0 beta 03</copyright> <language>zh-cn</language><item><title>Difference between DataContext and ItemsSource in </title><description><![CDATA[<p>今天在尝试SL的Binding时，发现对ListBox仅设置DataContext不能起到作用,XAML代码如下：</p>  <p><ListBox Grid.Row="2"    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Height="76"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HorizontalAlignment="Left"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Margin="311,35,0,0"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name="listBox_TemperatureList"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; VerticalAlignment="Top"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Width="219"     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DataContext="{StaticResource thermastatKey}"     <br /><font color="#ff0000"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ItemsSource="{Binding}</strong></font>">     <br />&#160;&#160;&#160; <ListBox.ItemTemplate>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <DataTemplate>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <StackPanel Orientation="Horizontal">&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <Image Source="{Binding TempImage}" Width="16" Height="16"/>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <TextBlock Text="{Binding Temperature,Converter={StaticResource temperatureConverterKey}}" />     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </StackPanel>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </DataTemplate>     <br />&#160;&#160;&#160; </ListBox.ItemTemplate>     <br /></ListBox></p>  <p>有问题找Google，发现有这个疑惑的还不少，参见<a href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/9bfdb32c-a4ce-46ad-b977-e12d51e815e2" target="_blank">.NET Framework Developer Center</a>得出的结论是：</p>  <p><font color="#ff0000">DataContext是所有FrameworkElement及其子类型的通用依赖属性。可以在逻辑树种从父节点继承，并且隐式的用于数据绑定。但是只有在ItemsSource属性制定后，WPF才会生成数据模板。而设置DataContext不会自动生成模板。</font></p>  <p>原文如：</p>  <p><font color="#ff0000">DataContext is a general (dependency) property of all descendants of FrameworkElement. Is is inherited through the logical tree from parent to children and can be used as an implicit source for DataBinding. It does not do anything by itself, you must basically databind to it.</font></p>]]></description><author>Jack</author><link>http://www.dongpad.com/CSharp-20100626-255.html</link><pubdate>2010-6-26 15:04:17</pubdate></item></channel></rss>
