Data binding to static resource: shown to work in VS but not when run
-
Hi, I'm following a Data Binding example from: microsoft[^] which displays the expected result in the VS2008 design window on XP but not when I run it. The XML looks OK to me with the XMLDataProver in a Resource :
<XmlDataProvider x:Key="MoreColors" XPath="/colors">
and the ListBox attribute :
ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}">
, any ideas why it displays data in the ListBox in design mode in VS2008 but doesn't display any data in the ListBox when its run?
<Window x:Class="WpfText2LableBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"><StackPanel> <StackPanel.Resources> <XmlDataProvider x:Key="MoreColors" XPath="/colors"> <x:XData> <colors > <color name="pink"/> <color name="white"/> <color name="black"/> <color name="cyan"/> <color name="gray"/> <color name="magenta"/> </colors> </x:XData> </XmlDataProvider> </StackPanel.Resources> <TextBlock Width="248" Height="24" Text="Colors:" TextWrapping="Wrap"/> <ListBox x:Name="lbColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}"> </ListBox> </StackPanel>
</Window>
-
Hi, I'm following a Data Binding example from: microsoft[^] which displays the expected result in the VS2008 design window on XP but not when I run it. The XML looks OK to me with the XMLDataProver in a Resource :
<XmlDataProvider x:Key="MoreColors" XPath="/colors">
and the ListBox attribute :
ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}">
, any ideas why it displays data in the ListBox in design mode in VS2008 but doesn't display any data in the ListBox when its run?
<Window x:Class="WpfText2LableBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"><StackPanel> <StackPanel.Resources> <XmlDataProvider x:Key="MoreColors" XPath="/colors"> <x:XData> <colors > <color name="pink"/> <color name="white"/> <color name="black"/> <color name="cyan"/> <color name="gray"/> <color name="magenta"/> </colors> </x:XData> </XmlDataProvider> </StackPanel.Resources> <TextBlock Width="248" Height="24" Text="Colors:" TextWrapping="Wrap"/> <ListBox x:Name="lbColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}"> </ListBox> </StackPanel>
</Window>
-
Thanks, you identified the cause though I think your linked solution is wrong (in this case anyway). It's now working but I had to use:
<x:XData> <colors xmlns="">
rather then the<x:XData xmlns=""> <colors >
in your link. I found another MS example which uses xmlns in this manner:<StackPanel.Resources>
<XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
<x:XData>
<Inventory xmlns="">
<Books>
<Book ISBN="0-7356-0562-9" Stock="in" Number="9">
<Title>XML in Action</Title>
<Summary>XML Web Technology</Summary>
</Book>