积极答复者
DataType="{x:Type WpfApplication1:BooleanTag}",不加x:Type就不行?

问题
-
我有个类BooleanTag,想把它加进ItemsControl里。为了呈现BooleanTag,我就写了个DataTemplate。
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" StartupUri="MainWindow.xaml"> <Application.Resources> <DataTemplate DataType="{x:Type WpfApplication1:BooleanTag}"> <TextBlock Text="{Binding Title}" Background="Khaki" Opacity="0.8" Margin="3" Height="20" Padding="1" /> </DataTemplate> </Application.Resources> </Application>
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1"> <ItemsControl Name="itemsControl"/> </Window>
using System; using System.Windows; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded_1(object sender, RoutedEventArgs e) { itemsControl.Items.Add(new BooleanTag { Title = "你好" }); } } public class BooleanTag { public string Title { get; set; } } }
以上代码是可以的,但如果把DataType="{x:Type WpfApplication1:BooleanTag}"写成DataType="WpfApplication1:BooleanTag",模板好像就不会自动应用了。
请问这是为什么?另外,为什么写style的时候,TargetType="Button"和TargetType="{x:Type Button}"似乎都可以?
答案
-
Hi,
根据MSDN文档中关于DateTemplate.DataType这一项的说明:If the template is intended for object data, this property contains the type name of the data object (as a string). To refer to the type name of the class, use the x:Type Markup Extension.
如果这个template是用于一个object,那么需要用可扩展标记型语言来指向这一object 类的类型名。即为: x:Type 类型名
而在Style.TargetType中,它原本的XAML使用方法就包括了两种:
<object TargetType="{x:Type typeName}"/> or <object TargetType="typeName"/>
-
你好,
严格来说,在XAML中只用类名而不用x:type指代一个类型是不合法的。
对于Style.TargetType,编译器有内置的转换设定,在编译时会做特殊转换,所以可以不加x:Type。
Min Zhu
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 爱让一切都对了 2013年1月6日 11:31
全部回复
-
Hi,
根据MSDN文档中关于DateTemplate.DataType这一项的说明:If the template is intended for object data, this property contains the type name of the data object (as a string). To refer to the type name of the class, use the x:Type Markup Extension.
如果这个template是用于一个object,那么需要用可扩展标记型语言来指向这一object 类的类型名。即为: x:Type 类型名
而在Style.TargetType中,它原本的XAML使用方法就包括了两种:
<object TargetType="{x:Type typeName}"/> or <object TargetType="typeName"/>
-
你好,
严格来说,在XAML中只用类名而不用x:type指代一个类型是不合法的。
对于Style.TargetType,编译器有内置的转换设定,在编译时会做特殊转换,所以可以不加x:Type。
Min Zhu
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 爱让一切都对了 2013年1月6日 11:31