Home > Enterprise >  Binding using XMLDataProvider in WPF not displaying data
Binding using XMLDataProvider in WPF not displaying data

Time:08-05

I am trying to bind some xml data to a listbox using WPF but i am not able to output anything. the xaml doesn't display any errors and the project builds and runs but no data is displayed in the listbox.

<Window x:Class="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:XMLDataProviderTutorial"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <XmlDataProvider x:Key="Products">
            <x:XData>
                <ROOT xmlns="">
                    <ITEM>Socks</ITEM>
                    <ITEM>Shoes</ITEM>
                    <ITEM>Toothbrush</ITEM>
                </ROOT>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <Grid>
        <ListBox 
            Background="Beige"
            ItemsSource="{Binding 
                Source={StaticResource Products}, 
                XPath='//ITEM'}"/>
    </Grid>
</Window>

CodePudding user response:

In the end, added in a listbox item template fixed the issue, for some reason it didn't update in the editor, had to run it to see the output

       <ListBox
            MinHeight="200"
            Background="Honeydew"
            ItemsSource="{Binding 
                Source={StaticResource Products}, 
                XPath='//ITEM'}">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding InnerText}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
  • Related