lloyd

In my application I want to display some data in a list box.
I need to display the data as a picture.
My 1st idea was to create a ValueConverter to convert my datas to ImageSource
But this has a few problem, the biggest one is there is no parameter!
So I though of MarkupExtension
something like that:
<ListBox ItemsSource={Binding Document.Maps}>
<ItemTemplate>
<Image Source="{local:Map2Image PreviewDpi=24, Background=false}" />
</ItemTemplate>
</ListBox>
but here, problem, in the Map2ImageExtension.ProvideValue(IServiceProvider) I don't ave the current data, worst, I have no idea to get it.
I was able to get the IProvideValueTarget but it only knows about the "Image" object, not about the current item.
I tried to look into the "Binding" and "BindingBase" code with reflector, but I don't understand what's going on...
Any tip
Otherwise I could write a specialized viewer FrameworkElement, but I though of using MarkupExtension might be a nice way to solve the problem....


Re: Windows Presentation Foundation (WPF) XAML extension question

Thomas C. Huber

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:

Code Snippet

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

Code Snippet

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






Re: Windows Presentation Foundation (WPF) XAML extension question

lloyd

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)





Re: Windows Presentation Foundation (WPF) XAML extension question

Rob Relyea

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





Re: Windows Presentation Foundation (WPF) XAML extension question

lloyd

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





Re: Windows Presentation Foundation (WPF) XAML extension question

Rob Relyea

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





Re: Windows Presentation Foundation (WPF) XAML extension question

ivolved_Mike_Brown

No the issue is that you've declared the ItemTemplate as the Content element of the ListBox. Try this instead

<ListBox ItemsSource={Binding Document.Maps}>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{local:Map2Image PreviewDpi=24, Background=false}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>





Re: Windows Presentation Foundation (WPF) XAML extension question

lloyd

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





Re: Windows Presentation Foundation (WPF) XAML extension question

Rob Relyea

any update





Re: Windows Presentation Foundation (WPF) XAML extension question

Rob Relyea

I'm going to morph the original post into a comment, since we are not sure if this answered or not.