seeker
seeker

Reputation: 6991

Newbie error: XamlParseException occured in WPF application

I'm trying to build a WPF application using the example here: Windows sample WPF application . However I'm getting the following error as I work through it

'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '36' and line position '55'.

And its thrown on the following line

 <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
         ItemTemplate="{StaticResource nameItemTemplate}">
        </ListBox>

The inner exception provides the following details

{"Cannot find resource named 'nameItemTemplate'. Resource names are case sensitive."}

Here's the problematic 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="300" d:DesignWidth="300"
    Title="ExpenseIt-Home">

    <Grid Margin="10,0,10,10">
        <Grid.Background>
            <ImageBrush ImageSource="2012-12-20 13.27.57.jpg"  />
        </Grid.Background>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="230" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- People list -->

        <Label Grid.Column="1" Style="{StaticResource headerTextStyle}" >
            View Expense Report
        </Label>

        <Border Grid.Column="1" Grid.Row="1" Style="{StaticResource listHeaderStyle}">
            <Label Style="{StaticResource listHeaderTextStyle}">Names</Label>
        </Border>
        <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
         ItemTemplate="{StaticResource nameItemTemplate}">
        </ListBox>



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

            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:XData>
                    <Expenses xmlns="">
                        <Person Name="Mike" Department="Legal">
                            <Expense ExpenseType="Lunch" ExpenseAmount="50" />
                            <Expense ExpenseType="Transportation" ExpenseAmount="50" />
                        </Person>
                        <Person Name="Lisa" Department="Marketing">
                            <Expense ExpenseType="Document printing"
      ExpenseAmount="50"/>
                            <Expense ExpenseType="Gift" ExpenseAmount="125" />
                        </Person>
                        <Person Name="John" Department="Engineering">
                            <Expense ExpenseType="Magazine subscription" 
     ExpenseAmount="50"/>
                            <Expense ExpenseType="New machine" ExpenseAmount="600" />
                            <Expense ExpenseType="Software" ExpenseAmount="500" />
                        </Person>
                        <Person Name="Mary" Department="Finance">
                            <Expense ExpenseType="Dinner" ExpenseAmount="100" />
                        </Person>
                    </Expenses>
                </x:XData>
            </XmlDataProvider>


            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>// isnt this enough?
            </DataTemplate> 
        </Grid.Resources>
    </Grid>
</Page>

Any help on what Im doing wrong and how I could correct it, would be greatly appreciated! Thanks!

Upvotes: 2

Views: 2323

Answers (2)

IdahoB
IdahoB

Reputation: 737

I ran into the same issue with the same example code - the term in the answer above "under the top level grid" was confusing to me, since the data provider WAS inside of the top level grid in the example, like this:

<Page>
  <Grid>
    <!-- other markup that uses the Resources -->

    <Grid.Resources>
      <!-- resources stuff as above -->
    </Grid.Resources>
  </Grid>
</Page>

The real issue is caused by the top-down parsing of the XAML code ...obviously a single-pass parser, not a two-pass parser (as is used for other markup). The solution, knowing this, is to move the grid static resources (data provider) so that it is physically ABOVE where it is referenced in the markup. Re-order the markup so it is more like the following and it will work ...this one was a puzzler:

<Page>
  <Grid>
    <Grid.Resources>
      <!-- resources stuff as above -->
      <!-- Grid.Resources MUST occur ABOVE the other markup that uses resources -->
    </Grid.Resources>

    <!-- other markup that uses the Resources -->
  </Grid>
</Page>

Enjoy!

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102753

This error indicates that a StaticResource key was not found. When you use static resources, they have to be defined prior to the reference, either in App.Xaml, or in the current file.

So, make sure that "ExpenseDataSource" and "nameItemTemplate" from the example are included under the top-level Grid.Resources:

<Page ...>
    <Grid ...>
        <Grid.Resources>
            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>

            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses"> 
                ...

        </Grid.Resources>

        <ListBox Name="peopleListBox" 
            ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
            ItemTemplate="{StaticResource nameItemTemplate}">

Upvotes: 4

Related Questions