学习WPF,主要是通过MSDN。即使这样,如果有一个不错的参考书还是不错的,可惜的是目前看来针对WPF的书籍不是很多。这本Chris Sells《Programming Windows Presentation Foundation》虽然出版时间已经一年了,但书中的内容还是适合WPF RTM版的。这本书和MSDN互相补充着学习,能起到事半功倍的效果(^_^)
对于此书的随书源码,本就组织得不错。但本着学以致用的原则,“不辞辛苦”(主要是代码整理工作比较繁琐)把这些随书各个章节的源码重新整理编辑在一起,其间参考了windows sdk中SampleGallery的页面设计布局,本想试用一下最新的Express Blend beta1,但看着现在它黑色的themes实在别扭,最后还是用手工写的xaml文件。
把此书的目录和例子编辑成一个xml文件,片段如下:
<ChapterTitle Name="Chapter4. DataBinding">
<SampleTitle Name="PersonBinding" Xaml="Samples\ch4\window4.xaml" Csharp="" />
<SampleTitle Name="MasterDataBinding" Xaml="Samples\ch4\window2.xaml" Csharp="" />
<SampleTitle Name="ObjectPersonBinding" Xaml="Samples\ch4\window3.xaml" Csharp="" />
<SampleTitle Name="CustomPersonBinding" Xaml="Samples\ch4\window1.xaml" Csharp="" />
<SampleTitle Name="XmlPersonBinding" Xaml="Samples\ch4\window5.xaml" Csharp="" />
</ChapterTitle>
<SampleTitle Name="PersonBinding" Xaml="Samples\ch4\window4.xaml" Csharp="" />
<SampleTitle Name="MasterDataBinding" Xaml="Samples\ch4\window2.xaml" Csharp="" />
<SampleTitle Name="ObjectPersonBinding" Xaml="Samples\ch4\window3.xaml" Csharp="" />
<SampleTitle Name="CustomPersonBinding" Xaml="Samples\ch4\window1.xaml" Csharp="" />
<SampleTitle Name="XmlPersonBinding" Xaml="Samples\ch4\window5.xaml" Csharp="" />
</ChapterTitle>
程序运行后在Application中设置动态绑定xml文件作为DataProvider,如下:
<XmlDataProvider x:Key="Book" Source="Book.xml" XPath="Book" />
在UI页面中绑定不同的UI控件的属性,片段如下:
<Expander Style="{StaticResource ExpanderStyler}" DataContext="{Binding Source={StaticResource Book},XPath=ChapterTitle[1]}">
<Expander.Header>
<TextBlock Style="{StaticResource ExpanderHeaderTextStyler}" />
</Expander.Header>
<ListBox Style="{StaticResource listBoxStyler}"/>
</Expander>
<Expander.Header>
<TextBlock Style="{StaticResource ExpanderHeaderTextStyler}" />
</Expander.Header>
<ListBox Style="{StaticResource listBoxStyler}"/>
</Expander>
最终运行效果图如下:
更详细的部分参见源代码
最后,你可以在FlazX这儿找到这本书,学习WPF的朋友大家多交流交流!