简体   繁体   English

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

[英]How do I populate a listbox with xaml using an xml document

I am trying to populate a listbox using xaml. 我正在尝试使用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. 我尝试过移动xml文件的绑定。 The xaml: 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>

Try changing Element and Elements with the following 尝试使用以下方法更改Element和Elements

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM