none
DataType="{x:Type WpfApplication1:BooleanTag}",不加x:Type就不行? RRS feed

  • 问题

  • 我有个类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}"似乎都可以?

    2013年1月2日 5:47

答案

全部回复