XmlDataProvider
-
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>...
</PicDbThe 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?
-
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>...
</PicDbThe 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?
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
-
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>...
</PicDbThe 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?
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.
-
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>...
</PicDbThe 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?
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.
-
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.