Wpf 如何使用DataTemplates解决工具栏错误?

Wpf 如何使用DataTemplates解决工具栏错误?,wpf,.net-4.0,datatemplate,toolbar,Wpf,.net 4.0,Datatemplate,Toolbar,(Wpf,.Net4) 哎,, 当我使用带有数据类型(没有键)的数据模板时,它只适用于构造函数。 (我必须这样使用它,因为我有两种不同的类型)。 问题是,它在.NET3.5上运行,现在我需要升级到.NET4。(我对项目的属性进行了研究,发现它正在3.5版上运行) 这是完整的代码(默认App.xaml除外): MainWindow.xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.

(Wpf,.Net4)

哎,, 当我使用带有数据类型(没有键)的数据模板时,它只适用于构造函数。 (我必须这样使用它,因为我有两种不同的类型)。 问题是,它在.NET3.5上运行,现在我需要升级到.NET4。(我对项目的属性进行了研究,发现它正在3.5版上运行) 这是完整的代码(默认App.xaml除外):

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1" x:Name="me"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ToolBar ItemsSource="{Binding ElementName=me, Path=Items}">
            <ToolBar.Resources>
                <DataTemplate  DataType="{x:Type local:Class1}">
                    <TextBlock Text="{Binding Title}" />
                </DataTemplate>
            </ToolBar.Resources>
        </ToolBar>

        <StackPanel Grid.Row="1">
            <Button Content="1"  Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Window>

MainWindow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Items = new ObservableCollection<Class1>();
            Items.Add(new Class1() { Title = "1" });

            InitializeComponent();
        }

        public ObservableCollection<Class1> Items { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Items.Add(new Class1() { Title = (Items.Count + 1).ToString() });
        }
    }

    public class Class1
    {
        public string Title { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
命名空间WpfApplication1
{
公共部分类主窗口:窗口
{
公共主窗口()
{
Items=新的ObservableCollection();
添加(新的Class1(){Title=“1”});
初始化组件();
}
公共ObservableCollection项{get;set;}
私有无效按钮\u单击(对象发送者,路由目标e)
{
Add(new Class1(){Title=(Items.Count+1).ToString()});
}
}
公共班级1
{
公共字符串标题{get;set;}
}
}
按钮动态添加项目,我们可以看到第一个显示其标题,动态添加的项目没有应用模板。
我能做什么?谢谢。

显然,在呈现
之后,工具栏
不再使用默认的
Class1
模板。根据需求,这里有几种解决方案可以工作

1)在App.xaml中定义
DataTemplate

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <DataTemplate DataType="{x:Type TestWPF:Class1}">
                    <TextBlock Text="{Binding Title}" />
                </DataTemplate>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
3)ItemControls的标准WPF方法-拥有
ItemTemplate

 <ToolBar ItemsSource="{Binding ElementName=me, Path=Items}">
     <ToolBar.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Title}" />
         </DataTemplate>
     </ToolBar.ItemTemplate>
 </ToolBar>


但是您说过有几种类型应该有模板,因此要使其工作,您必须为
工具栏
实现此问题的第四种解决方法-您需要使用另一个集合来形成新的工具栏内容,然后将此集合的值分配给项目

谢谢,使用资源字典的解决方案有效。我尝试了ItemTemplateSelector解决方案,但它要求我将DataTemplate放在App.xaml资源中,因为它在ApplyTemplate方法上没有DataTemplate。另外,请您解释一下,当模板在App.xaml上而不是在窗口资源中时,为什么它会应用该模板?ItemTemplateSelector没有那么严格。你可以找到很多教程。至于数据模板的位置-我不确定。有空的时候我会好好研究的。非常感谢。你帮了大忙:-)
 <ToolBar ItemsSource="{Binding ElementName=me, Path=Items}">
     <ToolBar.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Title}" />
         </DataTemplate>
     </ToolBar.ItemTemplate>
 </ToolBar>