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. XmlDataProvider

XmlDataProvider

Scheduled Pinned Locked Moved WPF
5 Posts 4 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.
  • M Offline
    M Offline
    mikla521
    wrote on last edited by
    #1

    I have a DataTemplate like this: (more actually...)

    <DataTemplate x:Key="ProductListTemplate">
    <Expander Name="ProductExpander" Header="{Binding Path=ProductName}">

    <TextBlock Text="{Binding Path=ProductId}"></TextBlock>
    <Image></Image>

    </Expander>
    </Border>
    </DataTemplate>

    ... a ListBox like this:

    <ListBox Name="productListbox"
    ItemTemplate="{StaticResource ProductListTemplate}"
    </ListBox>

    Now the tricky part, if it ever can be solved? How can I use an XmlDataProvider to populate Image source? The Listbox contains a lot more products than necessary amount of pictures. Same picture for several products. The xml file:

    <PicDb>
    <Product Name="W705">
    <Image>graphics/W705.png</Image>
    </Product>
    <Product Name="W715">
    <Image>graphics/W705.png</Image>
    </Product>

    ...
    </PicDb

    The Expander header binding and textbox binding works fine, binds to a data class. Can't figure out how to do it :( Anyone? Do you get the idea? Let's say that the expanders header result in a string "W705", then I want to use the xml file as a lookuptable and load the expanders image with "graphics/W705.png". But it doesn't seem to work with a mix of binding like that?

    H P A 3 Replies Last reply
    0
    • M mikla521

      I have a DataTemplate like this: (more actually...)

      <DataTemplate x:Key="ProductListTemplate">
      <Expander Name="ProductExpander" Header="{Binding Path=ProductName}">

      <TextBlock Text="{Binding Path=ProductId}"></TextBlock>
      <Image></Image>

      </Expander>
      </Border>
      </DataTemplate>

      ... a ListBox like this:

      <ListBox Name="productListbox"
      ItemTemplate="{StaticResource ProductListTemplate}"
      </ListBox>

      Now the tricky part, if it ever can be solved? How can I use an XmlDataProvider to populate Image source? The Listbox contains a lot more products than necessary amount of pictures. Same picture for several products. The xml file:

      <PicDb>
      <Product Name="W705">
      <Image>graphics/W705.png</Image>
      </Product>
      <Product Name="W715">
      <Image>graphics/W705.png</Image>
      </Product>

      ...
      </PicDb

      The Expander header binding and textbox binding works fine, binds to a data class. Can't figure out how to do it :( Anyone? Do you get the idea? Let's say that the expanders header result in a string "W705", then I want to use the xml file as a lookuptable and load the expanders image with "graphics/W705.png". But it doesn't seem to work with a mix of binding like that?

      H Offline
      H Offline
      hb52134214
      wrote on last edited by
      #2

      This might offer some ideas: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/wpf_conceptual/html/7dcd018f-16aa-4870-8e47-c1b4ea31e574.htm If PicDb is a separate data struture, maybe binding to a method and passing the name could work. In that case: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/wpf_conceptual/html/5f55e71e-2182-42a0-88d1-700cc1427a7a.htm

      1 Reply Last reply
      0
      • M mikla521

        I have a DataTemplate like this: (more actually...)

        <DataTemplate x:Key="ProductListTemplate">
        <Expander Name="ProductExpander" Header="{Binding Path=ProductName}">

        <TextBlock Text="{Binding Path=ProductId}"></TextBlock>
        <Image></Image>

        </Expander>
        </Border>
        </DataTemplate>

        ... a ListBox like this:

        <ListBox Name="productListbox"
        ItemTemplate="{StaticResource ProductListTemplate}"
        </ListBox>

        Now the tricky part, if it ever can be solved? How can I use an XmlDataProvider to populate Image source? The Listbox contains a lot more products than necessary amount of pictures. Same picture for several products. The xml file:

        <PicDb>
        <Product Name="W705">
        <Image>graphics/W705.png</Image>
        </Product>
        <Product Name="W715">
        <Image>graphics/W705.png</Image>
        </Product>

        ...
        </PicDb

        The Expander header binding and textbox binding works fine, binds to a data class. Can't figure out how to do it :( Anyone? Do you get the idea? Let's say that the expanders header result in a string "W705", then I want to use the xml file as a lookuptable and load the expanders image with "graphics/W705.png". But it doesn't seem to work with a mix of binding like that?

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

        If you don't want to go the whole MVVM route, a simple fix is to use a ValueConverter here. Consider the following sample converter code:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Windows.Data;
        using System.Windows.Media.Imaging;

        namespace SampleWpfApplication
        {
        public class SimpleImageConverter : IValueConverter
        {

        #region IValueConverter Members
        
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          Uri uri = new Uri(value.ToString(), UriKind.Absolute);
          BitmapImage img = new BitmapImage(uri);
          return img;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          throw new NotImplementedException();
        }
        
        #endregion
        

        }
        }

        To use the converter, you add a reference to it in your Resources

        section:<local:SimpleImageConverter x:Key="MyConverter" />

        Finally, you bind to it using:<Image Source="{Binding ImageItem, Converter={StaticResource MyConverter}}" />It's that simple.

        "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

        1 Reply Last reply
        0
        • M mikla521

          I have a DataTemplate like this: (more actually...)

          <DataTemplate x:Key="ProductListTemplate">
          <Expander Name="ProductExpander" Header="{Binding Path=ProductName}">

          <TextBlock Text="{Binding Path=ProductId}"></TextBlock>
          <Image></Image>

          </Expander>
          </Border>
          </DataTemplate>

          ... a ListBox like this:

          <ListBox Name="productListbox"
          ItemTemplate="{StaticResource ProductListTemplate}"
          </ListBox>

          Now the tricky part, if it ever can be solved? How can I use an XmlDataProvider to populate Image source? The Listbox contains a lot more products than necessary amount of pictures. Same picture for several products. The xml file:

          <PicDb>
          <Product Name="W705">
          <Image>graphics/W705.png</Image>
          </Product>
          <Product Name="W715">
          <Image>graphics/W705.png</Image>
          </Product>

          ...
          </PicDb

          The Expander header binding and textbox binding works fine, binds to a data class. Can't figure out how to do it :( Anyone? Do you get the idea? Let's say that the expanders header result in a string "W705", then I want to use the xml file as a lookuptable and load the expanders image with "graphics/W705.png". But it doesn't seem to work with a mix of binding like that?

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          If you see Pete's post, you could just read your XML file in the converter and get the image name for the passed in header string. This will avoid you having to bother about multiple datasources in XAML. If you still want to use XMLDataProvider, you could use a MultiValueConverter. Passing in the XMLDataProvider and the Header string to the converter.

          M 1 Reply Last reply
          0
          • A ABitSmart

            If you see Pete's post, you could just read your XML file in the converter and get the image name for the passed in header string. This will avoid you having to bother about multiple datasources in XAML. If you still want to use XMLDataProvider, you could use a MultiValueConverter. Passing in the XMLDataProvider and the Header string to the converter.

            M Offline
            M Offline
            mikla521
            wrote on last edited by
            #5

            Helped me a lot!! Thanks!

            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