1

I'm having some problems with data binding in XAML and WPF. Specifically, I'm trying to bind data from an XmlDataProvider to a ListBox.

The problem is this, when I'm in design mode in Visual Studio 2010, the xml items show up correctly, but when I run the app the listbox is just empty.

Here is what my xaml looks like. I'm not using any code behind, so this is all there is:

<Window x:Class="WpfTest9_Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="309" Width="622">
    <Window.DataContext>
        <XmlDataProvider XPath="Servers">
            <x:XData>
                <Servers>
                    <Server name="Server01" active="true" />
                    <Server name="Server02" active="false" />
                    <Server name="Testserver01" active="true" />
                    <Server name="Testserver02" active="true" />
                </Servers>
            </x:XData>
        </XmlDataProvider>
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding XPath=*}" Margin="12">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border CornerRadius="5" Margin="5" BorderThickness="2" BorderBrush="#FFC14343">
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <CheckBox IsChecked="{Binding XPath=@active}" />
                            <Label Content="{Binding XPath=@name}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Like I said above, the strange thing is that it looks like it's working while in design mode, but it fails to fill the listbox when I run the application. I'm not getting any error messages or warnings either.

What's wrong?

luddet
  • 1,235
  • 1
  • 11
  • 11

1 Answers1

1

Ok, the solution was surprisingly simple. As pointed out in this post, Listbox content not being populated with content from xml when using XmlDataProvider , all I had to do was to add an empty namespace attribute to the xml element. Like this:

<Servers xmlns="">
    <Server name="Server01" active="true" />
    <!-- ... -->
</Servers>
Community
  • 1
  • 1
luddet
  • 1,235
  • 1
  • 11
  • 11