为什么列表框ItemTemplateSelector =“ {StaticResource MyDataTemplateSelector}”无法与资源一起使用?

时间:2019-01-09 12:55:11

标签: c# datatemplate itemtemplateselector

主题:我正在使用Microsoft示例来学习DataTemplate概念,该示例位于https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview,在GitHub上位于https://github.com/Microsoft/WPF-Samples/tree/master/Data%20Binding/DataTemplatingIntro

问题:“ Xaml设计”窗口显示错误。

错误: NullReferenceException:对象引用未设置为对象的实例。

从堆栈顶部开始例外:

在BindingTest.TaskListDataTemplateSelector.SelectTemplate(对象项,DependencyObject容器)

具体: 创建类TaskListDataTemplateSelector并添加以下两行后,将出现错误:

<Window.Resources>

    <local:TaskListDataTemplateSelector x:Key="MyDataTemplateSelector"/>

</Window.Resources>

<ListBox ....

   ItemTemplateSelector="{StaticResource MyDataTemplateSelector}"

</ListBox>

更多信息: Microsoft的示例也有同样的问题。

Window x:Class="BindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:BindingTest"
    mc:Ignorable="d"
    Title="MainWindow"  Width="525" SizeToContent="WidthAndHeight">

<Window.Resources>
    <local:Tasks x:Key="myTodoList"/>
    <local:TaskListDataTemplateSelector x:Key="MyDataTemplateSelector"/>

    <DataTemplate x:Key="importantTaskTemplate">
        <DataTemplate.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="20"/>
            </Style>
        </DataTemplate.Resources>
        <Border Name="border" BorderBrush="Red" BorderThickness="1"
                Padding="5" Margin="5">
            <DockPanel HorizontalAlignment="Center">
                <TextBlock Text="{Binding Path=Description}"/>
                <TextBlock>!</TextBlock>
            </DockPanel>
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="myTaskTemplate">
        <Border Name="border" BorderBrush="Aqua" BorderThickness="1"
               Padding="5" Margin="5">
            <Grid ShowGridLines="false">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0"  Text="Task Name:"/>
                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}"/>
                <TextBlock Grid.Row="1" Grid.Column="0"  Text="Description:"/>
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}"/>
                <TextBlock Grid.Row="2" Grid.Column="0"  Text="Priority:"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Priority}"/>
            </Grid>
        </Border>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=TaskType}">
                <DataTrigger.Value>
                    <local:TaskType>Home</local:TaskType>
                </DataTrigger.Value>
                <Setter TargetName="border" Property="BorderBrush" Value="Yellow"/>
            </DataTrigger>
        </DataTemplate.Triggers>

    </DataTemplate>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBlock FontSize="20" Text="My Task List"/>

        <ListBox Width="400" Margin="10"
                 ItemsSource="{Binding Source={StaticResource myTodoList}}"
                 ItemTemplateSelector="{StaticResource MyDataTemplateSelector}"
                   HorizontalContentAlignment="Stretch">
        </ListBox>
    </StackPanel>

</Grid>

 public   class TaskListDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate
      SelectTemplate(object item, DependencyObject container)
    {
        if (item != null && item is Task)
        {
            var taskitem = (Task)item;
            var window = Application.Current.MainWindow;
            if (taskitem.Priority == 1)
                return
                    window.FindResource("ImportantTaskTemplate") as DataTemplate;
            return
                window.FindResource("MyTaskTemplate") as DataTemplate;
        }

        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了一个错误。这是一个拼写问题:大写。

在整个项目中,MyTaskTemplate和ImportantTaskTemplate的拼写不一致。

该项目现在可以编译并运行,但是在Xaml Design窗口中仍然会出现相同的异常。

更多挖掘工作揭示了Xaml设计窗口中的Null Reference Exception错误的修复程序。感谢Chris Anderson和他的书:Essential Windows Presentation Foundation页面341。用以下内容替换TaskListDataTemplateSelector:

 public class TaskListDataTemplateSelector : DataTemplateSelector
{

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item != null && item is Task)
        {
            var taskitem = (Task)item;
            var window = Application.Current.MainWindow;
            if (taskitem.Priority == 1)
                return ((FrameworkElement)container).FindResource
                    ("ImportantTaskTemplate") as DataTemplate;

            return
            ((FrameworkElement)container).FindResource
                    ("MyTaskTemplate") as DataTemplate;
        }
        return null;
    }
}