简体   繁体   中英

Items doesn't show up after binding an XML file to a ListBox

I want to bind XML file to a ListBox. The problem is that items in ListBox doesn't show up after binding it to an XML file.

I've set ItemsSource in ListBox to StaticResource but it doesn't work, it doesn't show up in Visual Studio's Designer or in app itself.

Here's XAML code:

<Window x:Class="StudyNotes.ModifySubjectListWindow"
        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:StudyNotes"
        mc:Ignorable="d"
        Title="" Height="150" Width="300" ResizeMode="NoResize">
    <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="SubjectData" Source="SubjectList.xml" XPath="/Subjects/Subject"/>
        </Grid.Resources>
        <DockPanel Margin="10">
            <StackPanel DockPanel.Dock="Right" Margin="10,0,0,0">
                <Button Name="AddSubjectButton" Margin="0,0,0,5">Add</Button>
                <Button Name="DeleteSubjectButton">Delete</Button>
            </StackPanel>
            <ListBox x:Name="SubjectList" ItemsSource="{Binding Source={StaticResource SubjectData}, XPath=/Subjects/Subject}"></ListBox>
        </DockPanel>
    </Grid>
</Window>

Here's XML document:

<?xml version="1.0" encoding="utf-8" ?>
<Subjects>
  <Subject Name="Subject1"/>
  <Subject Name="Subject2"/>
  <Subject Name="Subject3"/>
  <Subject Name="Subject4"/>
</Subjects>

I expected this to be working and showing up, but there's definitely something wrong that I have no idea of.

There are a couple of items:

First of all, make sure that your 'SubjectList.xml' file has its Build Action property set to 'Content'.

Secondly, remove the 'XPath' stuff from the ItemsSource of your ListBox , that causes some trouble. You only need ItemsSource="{Binding Source={StaticResource SubjectData}}"

Thirdly, and this is the big one, your XML file is not set up quite right. After you make the two changes above, change your ListBox to a DataGrid for a quick test and it will highlight the trouble with the XML file:

在此处输入图片说明

It's kind of hard to see in this screenshot, but check out the 'Value' column. It's empty. Your XML file is set up where your data is stored in 'Attributes', specifically the 'Name' attribute, as you can see if you look over in the 'OuterXML' column. The XMlDataProvider grabs the Value s in the XML file by default. You don't have any of those.

A better way to store your XML data might be:

<Subjects>
  <Subject>Subject1</Subject>
  <Subject>Subject2</Subject>
  <Subject>Subject3</Subject>
  <Subject>Subject4</Subject>
</Subjects>

If you do that, you get the expected result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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