C# 使用WPF时的数据绑定问题

C# 使用WPF时的数据绑定问题,c#,wpf,xaml,C#,Wpf,Xaml,我一直在努力学习维索尔C。最近,我一直在关注WPF。以下是我一直在编写的教程的链接: 如果您选择某人的姓名并单击“查看”按钮,应用程序应允许您查看其费用。不幸的是,费用没有显示出来。我一直收到以下错误消息: System.Windows.Data Error: 50 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns=""). 我认为xmlns应该留空,这样应用程序就可以在X

我一直在努力学习维索尔C。最近,我一直在关注WPF。以下是我一直在编写的教程的链接:

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

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>
谁能告诉我出了什么问题吗


谢谢

在《WPF 4.5释放》一书中,亚当·内森给出了这样一个示例。 xml:

xaml:


hth

原来问题不在于网格资源!我忘了在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后,数据将正确显示。

您需要使用XmlNamespaceManager。从《每当XPath值没有前缀时释放的WPF4.5》一书中,空名称空间被假定为名称空间URI。因此,即使XML源具有默认名称空间,也必须为查询分配一个XmlNamespaceManager。
<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>
<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>