Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Data binding to static resource: shown to work in VS but not when run

Data binding to static resource: shown to work in VS but not when run

Scheduled Pinned Locked Moved WPF
wpfxmlvisual-studiowcfcom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonathan Davies
    wrote on last edited by
    #1

    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>

    P 1 Reply Last reply
    0
    • J Jonathan Davies

      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>

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I answered this here[^]. Add the namespace I mention in the answer and you are good to go.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      J 1 Reply Last reply
      0
      • P Pete OHanlon

        I answered this here[^]. Add the namespace I mention in the answer and you are good to go.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        J Offline
        J Offline
        Jonathan Davies
        wrote on last edited by
        #3

        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>

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups