书名:WPF专业编程指南
作者:李应保
出版社:电子工业出版社
出版时间:2010-01
ISBN:9787121100116
一、静态资源扩展
- (StaticResourceExtension)
前面已经用了StaticResource标记扩展,这个扩展用来获取静态属性值,这是XAML编译器在编译时完成的。
二、例子
<Window x:Class="Yingbao.Chapter2.SimpleBindingEx.AppWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Static Markup Extension" Height="100" Width="200">
<Window.Resources>
<sys:String x:Key="personName">李白</sys:String>
</Window.Resources>
<Grid>
<TextBlock Width="200" Height="50" Text="{Binding
Source={StaticResource personName}}"/>
</Grid>
</Window>
- C#代码是Visual Studio产生的:
namespace Yingbao.Chapter2.SimpleBindingEx
{
public partial class AppWin : Window
{
public AppWin()
{
InitializeComponent();
}
}
}
-
在窗口的资源部分定义了一个简单的字符串李白,注意字符串位于.NET平台的System命名空间中;因此,在window标记中加了xmlns:sys。
这段XAML的运行结果如图2-1所示。
图2-1 使用静态资源标记扩展
