使用WPF时数据绑定的问题

时间:2014-06-11 23:02:07

标签: c# wpf xaml

我一直在努力学习Visaul C#。最近,我一直专注于WPF。这是我一直在研究的教程的链接:

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

如果您选择其名称并单击“查看”按钮,应用程序应该允许您查看某人的费用。不幸的是,费用没有显示出来。我不断收到此错误消息:

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

我认为xmlns需要留空,因此应用程序可以向下导航XAML。这是我做的网格资源的代码:

<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>

任何人都可以给我任何关于出错的提示吗?

由于

2 个答案:

答案 0 :(得分:1)

原来问题不在于网格资源!我忘了在DataGrid中设置ItemsSource。在我的ExpenseReportPage.xaml文件中,我输入了以下内容:

<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>

设置ItemsSource后,数据显示正确。

答案 1 :(得分:0)

在WPF 4.5 Unleashed一书中,他(Adam Nathan)给出了一个类似的样本。 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