在WPF的XAML中,将数据从XmlDataProvider绑定到ListBox失败

本文关键字:XmlDataProvider 绑定 失败 ListBox 数据 WPF XAML | 更新日期: 2023-09-27 18:08:33

最近我开始学习WPF。在学习绑定数据时,我创建了一个listbox,并将XmlDataProvider中的数据绑定到它。以下是XmlDataProvider的代码:

<Grid.Resources>
......
<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>
......
</Grid.Resources>

这里是Listbox的代码:

<Grid>
......
<ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}">
<!-- Name item template -->
    <DataTemplate>
        <Label Content="{Binding XPath=@Name}"/>
    </DataTemplate>
</ListBox>
......
<Grid>

当我编译并运行代码时,ListBox不包含任何内容。我试图从MSDN找到解决方案,但无法理解错误在哪里。

在WPF的XAML中,将数据从XmlDataProvider绑定到ListBox失败

这至少可以让您入门。我建议你先从简单一点的问题开始,然后逐步解决比较困难的问题。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <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>
        </Grid.Resources>
        <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         DataContext="{Binding Source={StaticResource ExpenseDataSource}}" ItemsSource="{Binding Name}"/>
    </Grid>
</Window>

我修剪了你的Listbox,没有数据模板的标签(实际上你可以用数据模板代替Listbox中的堆栈面板)。我将列表框的数据上下文设置为静态资源,并将itemssource设置为绑定名称。它当前将把单词Expenses显示为一个单独的字符串。