簡體   English   中英

在WPF中的XAML中,將數據從XmlDataProvider綁定到ListBox失敗

[英]Binding data from XmlDataProvider to a ListBox fails in XAML in WPF

最近,我開始學習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查找解決方案,但無法理解錯誤所在。

這至少應該使您入門。 我建議您從更簡單的角度出發,然后逐步找到更困難的解決方案。

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

我修剪了列表框,使其沒有帶有datatemplate的標簽(實際上,您可以將數據模板替換為Listbox中的堆棧面板。我將列表框的數據上下文設置為靜態資源,並將itemsource設置為Binding Name。當前將單詞“費用”顯示為單個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM