错误 - 无法在WPF应用程序中找到静态资源

时间:2010-05-26 15:11:25

标签: .net wpf xaml .net-4.0

我正在学习WPF并从this MSDN教程开始。

我刚刚学习了这个教程。当我按照教程完成代码并尝试运行时,我在XAML页面中得到一个异常

  

'在'System.Windows.StaticResourceExtension'上提供值引发异常。行号“27”和行位置“55”。“。内部异常显示错误是”找不到名为'personItemTemplate'的资源。资源名称区分大小写。“。

罪魁祸首XAML在下面。

<Page x:Class="ExpenseIt.ExpenseItHome"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="321" d:DesignWidth="532"
    Title="ExpenseIt - Home">

    <Grid Margin="10,0,10,10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="230" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Grid.Column="1" Style="{StaticResource headerTextStyle}">View Expense Report</Label>
        <!-- Resource List Label-->
        <Border Grid.Column="1" Grid.Row="1" Style="{StaticResource listHeaderStyle}">
            <Label VerticalAlignment="Center" Foreground="White" FontWeight="Bold">Names</Label>
        </Border>
        <!-- Resource List-->
        <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
         ItemTemplate="{StaticResource personItemTemplate}">
        </ListBox>

        <!-- View button -->
        <Button Grid.Column="1" Grid.Row="3" Click="Button_Click" Style="{StaticResource buttonStyle}">View</Button>

        <!-- Set Background Image-->
        <Grid.Background>
            <ImageBrush ImageSource="watermark.png" />
        </Grid.Background>
        <Grid.Resources>

            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:XData>
                    <Expenses xmlns="">
                        <Person Name="TommyVance" Department="Legal">
                            <Expense ExpenseType="Lunch" ExpenseAmount="50" />
                            <Expense ExpenseType="Transportation" ExpenseAmount="50" />
                        </Person>
                        <Person Name="PhilJackson" Department="Marketing">
                            <Expense ExpenseType="Document printing"
      ExpenseAmount="50"/>
                            <Expense ExpenseType="Gift" ExpenseAmount="125" />
                        </Person>
                        <Person Name="PaulBriggs" Department="Engineering">
                            <Expense ExpenseType="Magazine subscription" 
     ExpenseAmount="50"/>
                            <Expense ExpenseType="New machine" ExpenseAmount="600" />
                            <Expense ExpenseType="Software" ExpenseAmount="500" />
                        </Person>
                        <Person Name="AlfredNobel" Department="Finance">
                            <Expense ExpenseType="Dinner" ExpenseAmount="100" />
                        </Person>
                    </Expenses>
                </x:XData>
            </XmlDataProvider>
            <!-- Data Template to mention that Name should be fetched from the XMLDataProvider -->
            <!-- Name item template -->
            <DataTemplate x:Key="personItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>
        </Grid.Resources>
    </Grid>
</Page>

我在Grid资源中有所需的模板,因此将其添加为静态资源。但是,它抛出了datatemplate不可用的例外。

3 个答案:

答案 0 :(得分:28)

<Grid.Resources> ... </Grid.Resources>移动到网格定义的顶部,它将起作用。 DataTemplate似乎需要在引用之前定义。我将您的示例复制到一个应用程序中,并确认移动参考资料部分解决了这个问题。

答案 1 :(得分:3)

此错误有几个原因。我的问题的解决方案是我没有添加“InitializeComponent();”因此,在Application的构造函数中,从未初始化包含ResourceDictionary的Xaml。因此错误“找不到......”我没有提到我是手工编码。如果您通过Visual Studio生成代码,则不需要这样做。

答案 2 :(得分:0)

我遇到了同样的错误,但是以上答案均无效。

通过从以下位置更改数据模板上的XAML解决了我的错误:

<DataTemplate DataType="local:DtoDmParent" x:Key="dataTemplateDtoDmParent"  >
    <TextBlock Text="test"/>
</DataTemplate>

<DataTemplate x:Key="dataTemplateDtoDmParent" DataType="local:DtoDmParent" >
    <TextBlock Text="test"/>
</DataTemplate>

正在更改,因此Datatype参数位于x:key参数之后