Christopher Spears
Christopher Spears

Reputation: 1105

Issues with data binding while working with WPF

I have been trying to learn Visaul C#. Lately, I have been focusing on WPF. Here is the link to the tutorial I have been working on:

http://msdn.microsoft.com/en-us/library/vstudio/ms752299(v=vs.110).aspx

The application should let you view a person's expenses if you select their name and click the View button. Unfortunately, the expenses are not displayed. I keep getting this error message:

System.Windows.Data Error: 50 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").

I thought that xmlns needed to be left blank, so the app could navigate down the XAML. Here is the code for Grid resource that I made:

<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}"/>
            </DataTemplate>

</Grid.Resources>

Can anyone give me any hint about what is going wrong?

Thanks

Upvotes: 2

Views: 433

Answers (2)

Christopher Spears
Christopher Spears

Reputation: 1105

Turns out the issue was not with the Grid resource! I forgot to set ItemsSource in DataGrid. In my ExpenseReportPage.xaml file, I typed the following:

<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
                  HorizontalAlignment="Left">
                <!-- Expense type and Amount table -->
                <DataGrid ItemsSource="{Binding XPath=Expense}" ColumnHeaderStyle="{StaticResource columnHeaderStyle}" 
                          AutoGenerateColumns="False" RowHeaderWidth="0">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Expense Type" Binding="{Binding XPath=@ExpenseType}" />
                        <DataGridTextColumn Header="Amount" Binding="{Binding XPath=@ExpenseAmount}"/>
                    </DataGrid.Columns>
                </DataGrid>
</Grid>

After ItemsSource was set, the data was displayed properly.

Upvotes: 1

reggaeguitar
reggaeguitar

Reputation: 1804

In the book WPF 4.5 Unleashed he (Adam Nathan) gives a sample like so. xml:

<rss version = "2.0" xmlns:georss="http://www.gearss.org/georss">...</rss>

xaml:

<XmlDataProvider Source=http://api.twitter.com/1/statuses/user_timeline.rss?screen_name/adamnathan"
XmlNameSpaceManager="{StaticResource namespaceMapping}" XPath="rss/channel" x:Key="dataProvider" />

<XmllNamespaceMappingCollection x:Key="namespaceMapping">
    <XmlNamespaceMapping Uri="http://www.gearss.org/georss" Prefix="georss" />
</XmllNamespaceMappingCollection>

hth

Upvotes: 0

Related Questions