Hi lloyd,
what looks your data like. Only strings with a filepath and some additional information stored in each?
You should do it in a way like below:
<
Window x:Class="MyApp.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyApp" Title="MyApp" Height="300" Width="300" Loaded="WindowLoaded">
<
Window.Resources><
DataTemplate x:Key="image" DataType="{x:Type local:SimpleDataItem}"><
Image Width="100" Height="100" Source="{Binding ImagePath}"/></
DataTemplate></
Window.Resources><
ListBox x:Name="lb" ItemTemplate="{StaticResource image}"/></
Window>public partial class Window1 : System.Windows.Window
{
public Window1(){
InitializeComponent();
}
private void WindowLoaded(object sender, RoutedEventArgs e){
List<SimpleDataItem> data = new List<SimpleDataItem>(); foreach (string str in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.jpg"))data.Add(
new SimpleDataItem(str));lb.BeginInit();
lb.ItemsSource = data;
lb.EndInit();
}
}
public class SimpleDataItem{
private string _imagePath; public SimpleDataItem(string imagePath){
_imagePath = imagePath;
}
public string ImagePath{
get { return _imagePath; } set { _imagePath = value; }}
}
Is that helpful for you?
Thomas
my data looks like some proprietary vector graphics.
we have rendering code for WinForm/SD.Image/GDI+ but nothing for WPF (currently discovering WPF and using a WinFormHost for the rendering so far).
I'm writing a map selection (that is something where you could see multiple 'vector graphic at once' and choose one to edit).
it has now image (preview?) property and I like to keep it that way (useless drawing code otherwise)
now I try to render it in an WPF.Image through a custom made markup extension to convert my vector graphic format to an image.
I like to avoid ValueConverter as they have no parameter, MarkupExtension is perfect for the job except I don't know how to get the current item.
Anyway, meanwhile I give up and create a "MapPreview : FrameworkElement" and use data binding to set its value.
However, for the sake of increasing my knowledge I would like to know how to write usefull markup extension (one that could use data source in much the same way Binding markup extension does)
If you used IProvideValueTarget to get the Image object, I believe you should look at the datacontext on that object. My impression is that ItemsControl (in your case ListBox) works by explicitly setting the DataContext on each of the "items" it creates.
The datacontext of each image should be one record from the DataSource that you set on the ItemsControl...
Please let us know how that works for you!
Thanks, Rob
Rob Relyea | Program Manager, WPF & Xaml Language Team
robrelyea.com | /blog | /wpf | /xaml
Hi Rob,
Indeed I though of that this morning and quickly write a simple sample to put it to the test.
but I hit a 'strange' problem.
My extension is called only once at the load time of my window.
Even though later the user change the ListBox.ItemsSource by clicking on a Button, hence recreating a whole set of new item, my extension is not called again.
I'm probably missing something but.. I'm kind of stuck...
In the end, if you need something that can have an adapting value, you need to use one of the techniques to bind to either data or a resource name: {Binding} and {DynamicResource}. Those are both built on a currently internal type call Expression, which supports updatable values.
Since Expression is not currently public, you could figure out what you want to do, and then create the appropriate binding (see BindingOperations and/or FrameworkElement.SetBinding (if I recall correctly).
You should also look at ValueConverters that are used by Binding...to see if they might help here...
Hope that helps.
Thanks, Rob
Rob Relyea | Program Manager, WPF & Xaml Language Team
robrelyea.com | /blog | /wpf | /xaml
No the issue is that you've declared the ItemTemplate as the Content element of the ListBox. Try this instead
mmhh.... I moved on...
I think I will give it another though this week-end!
yeah, I just saw that Binding could be sepcifyed a particular converter, mmhh... which I could initialize as I see fit, mmhh...
I'm going to morph the original post into a comment, since we are not sure if this answered or not.