Syndicate

News

My name's Marco De Sanctis and I'm an IT professional from Italy. This is my technical blog, about .NET and related application development and design technologies.

Download my Resume (.doc)

Recent Comments

6/26/2008 at 12:21 AM

I really hope you could help me out on this. Say I...
by Roy

Read more...

6/14/2008 at 4:14 PM

Yep, it happened to me too. It violates the princi...
by Gian Maria

Read more...

6/13/2008 at 5:32 AM

Thank you very much, your postinge.on the coattail...
by cindex

Read more...

6/12/2008 at 1:06 PM

Okay. Maybe sometime I'll try to make my own, inhe...
by Lyr

Read more...

6/11/2008 at 4:09 PM

Hello Lyr,ImageSourceConverter is a TypeConverter ...
by Marco De Sanctis

Read more...

Recent Posts

Unleash the power of VisualStateManager with custom states

6/30/2008 at 12:12 AM

Read more...

Blend 2.5 June CTP and custom controls templating

6/25/2008 at 10:18 AM

Read more...

TransactionScope timeout

6/14/2008 at 12:01 AM

Read more...

Custom controls and default template in Silverlight 2 Beta 2

6/11/2008 at 11:37 AM

Read more...

Silverlight 2 Beta 2 Documentation Available

6/6/2008 at 10:17 AM

Read more...

Bind a Linq Binary type to ImageSource

posted on Thursday, May 08, 2008 12:37 AM | Filed Under [ WPF LINQ ]

If you need to store images at DB level, Sql Server varbinary type is what you need to hold your data, which is mapped on Linq Binary type once you import your table to a Linq project. How do you display such an image in a Image WPF control? Obviously you cannot directly bind such a data type to the Image.Source property, unless you define a proper value converter from Binary type to ImageSource type.

That said, if you want to implement one, you need to create a class that implements the IValueConverter interface. For the sake of simplicity, let's suppose that we need to support only one-way binding (Data field to Image.Source); our class might be similar to the one displayed below:

image

The logic is pretty simple: Linq's Binary type has a method which returns a byte array; what we need to do is only building a MemoryStream from it and use it to build a BitmapImage which is directly bindable to the Image.Source property.

Now, we can store our converter as a resource and use it to perform our XAML binding:

image

One last note: IValueConverter interface has a ConvertBack method too, which is needed to support two-way binding. In a next post, I'll show you how to create a two-way bindable UserControl with image upload features and we will need to implement the whole IValueConverter interface. For now, it's ok to leave it throwing a NotImplementedException.

image

Update: Here you will find the missing ConvertBack implementation

Technorati tags: , ,

Comments

Gravatar
# re: Bind a Linq Binary type to ImageSource
Posted by alkampfer on 5/9/2008 5:18 PM
Interesting post, I really think that binding structure of wpf is fantastic.
Gravatar
# re: thanks for your help
Posted by cindex on 6/13/2008 5:32 AM
Thank you very much, your postinge.
on the coattails of you, I solve broblem.
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 6 and 8 and type the answer here: