为使用未在WPF命名空间中定义的类,需要把.net命名空间映射 到一个XML命名空间
XAML有一个特殊的语法可用于完成这一工作。
语法:
xmlns:prefix="clr-namespace:Namespace;assembly=AssemblyName"
prefix:是希望在XAML标记中用于指示命名空间的XML前缀。例如,XAML语言使用的x前缀。通常情况下,使用.net中其他的类型,前缀使用sys,而使用当前项目中的类型,前缀会使用local命名。
Namespace:是完全限定的.net命名空间的名称。
AssemblyName:是声明类型的程序集,没有.dll扩展名。这个程序集必须在项目中引用。如果希望使用项目程序集,则可以忽略这一部分。
实例:
<Window x:Class="MyWPF._2_4使用其他命名空间中的类型"
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="_2_4使用其他命名空间中的类型" Height="300" Width="300">
<Grid>
<ListBox>
<ListBoxItem>
<sys:DateTime>10/13/2010 4:30</sys:DateTime>
</ListBoxItem>
<ListBoxItem>
<sys:DateTime>10/13/2010 4:30</sys:DateTime>
</ListBoxItem>
<ListBoxItem>
<sys:DateTime>10/13/2010 4:30</sys:DateTime>
</ListBoxItem>
<ListBoxItem>
<sys:DateTime>10/13/2010 4:30</sys:DateTime>
</ListBoxItem>
</ListBox>
</Grid>
</Window>