如何使用xml文档使用xaml填充列表框

时间:2022-07-21 07:12:22

I am trying to populate a listbox using xaml. I think I have the binding correct. I'm just not sure why the info isn't populating. I've tried moving the binding for the xml file around. The xaml:

我正在尝试使用xaml填充列表框。我想我的绑定是正确的。我只是不确定为什么信息不会填充。我试过移动xml文件的绑定。 xaml:

<Window x:Class="FinalProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="525">
    <Window.Background>
        <SolidColorBrush Color="LightBlue" />
    </Window.Background>
    <Window.DataContext>
        <XmlDataProvider x:Name="NorthWindTraders" Source="NorthWind_Database.xml" XPath="Customers" />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <!--Header-->
            <RowDefinition Height="Auto" />
            <!--Customer Selection-->
            <RowDefinition Height="Auto" />
            <!--Customer Details-->
        </Grid.RowDefinitions>

        <!--Header Row[0]-->
        <Label Content="My Final Project"
               Grid.Row="0" FontSize="24"
               HorizontalContentAlignment="Left"
               Padding="10"
               FontWeight="Bold" />
        <!--END Header-->

        <!--Customer Selection-->
        <Grid Grid.Row="1">
            <!--Customer Selection Defined into two parts, Selection and Sort-->
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <!--Customer Selection-->
                <RowDefinition Height="Auto" />
                <!--Sorting Order-->
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <!--Selection Populated by Database-->
            <Label Content="Select Customer:"
                   Grid.Column="0" />
            <ListBox x:Name="NorthWindListBox" 
                     ItemsSource="{Binding}"
                     IsSynchronizedWithCurrentItem="True"
                     Visibility="Visible"
                     SelectionMode="Single"
                     Grid.Column="1"
                     Width="250"
                     Height="100" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding XPath=CompanyName}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <!--Add list box items-->
        </Grid>
    </Grid>
</Window>

sample from the xml file:

来自xml文件的示例:

<?xml version="1.0" encoding="utf-8" ?>
<NorthWind>
  <Customers>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria Anders</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Berlin</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
  </Customers>
  <Customers>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <ContactName>Ana Trujillo</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>Avda. de la Constitución 2222</Address>
    <City>México D.F.</City>
    <PostalCode>05021</PostalCode>
    <Country>Mexico</Country>
    <Phone>(5) 555-4729</Phone>
    <Fax>(5) 555-3745</Fax>
  </Customers>

1 个解决方案

#1


0  

Try changing Element and Elements with the following

尝试使用以下内容更改元素和元素

    <Window.DataContext>
        <XmlDataProvider x:Key="NorthWindTraders" Source="NorthWind_Database.xml"/>
    </Window.DataContext>


    <ListBox x:Name="NorthWindListBox" 
                     ItemsSource="{ Binding Source={NorthWindTraders}, XPath=NorthWind/Customers}"
                     IsSynchronizedWithCurrentItem="True"
                     Visibility="Visible"
                     SelectionMode="Single"
                     Grid.Column="1"
                     Width="250"
                     Height="100" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding XPath=CompanyName}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

#1


0  

Try changing Element and Elements with the following

尝试使用以下内容更改元素和元素

    <Window.DataContext>
        <XmlDataProvider x:Key="NorthWindTraders" Source="NorthWind_Database.xml"/>
    </Window.DataContext>


    <ListBox x:Name="NorthWindListBox" 
                     ItemsSource="{ Binding Source={NorthWindTraders}, XPath=NorthWind/Customers}"
                     IsSynchronizedWithCurrentItem="True"
                     Visibility="Visible"
                     SelectionMode="Single"
                     Grid.Column="1"
                     Width="250"
                     Height="100" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding XPath=CompanyName}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>