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

7/28/2008 at 2:46 PM

Hi...What kind of problem are you experiencing?...
by Marco De Sanctis

Read more...

7/28/2008 at 12:59 PM

Very nice concept and can i expect for list box? ,...
by Sudhir Malireddy

Read more...

7/28/2008 at 9:58 AM

In aggregates the rula is: who is the rool element...
by Tommaso Caldarola

Read more...

7/22/2008 at 7:49 PM

Been there too in a past project! I remember this ...
by Mathias

Read more...

7/14/2008 at 11:50 PM

Sure I am... but if you got batches that last for ...
by Marco De Sanctis

Read more...

Recent Posts

Crunch mode is a pure waste of time, energy and money

7/31/2008 at 5:41 PM

Read more...

Double Click on the .sln file doesn't open Visual Studio on Vista

7/27/2008 at 9:02 AM

Read more...

Domain Model & Aggregates: when do master-detail associations happen?

7/22/2008 at 4:08 PM

Read more...

How I Got Started in Software Development

7/14/2008 at 12:16 AM

Read more...

Unleash the power of VisualStateManager with custom states

6/30/2008 at 12:12 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 4 and 3 and type the answer here: