WPF - Image control inside Listbox -


i displaying images inside list box. binding source of image control using object through datacontext of listbox.

problem: want display no image (image) if url does't have image. possible in xaml code.

code:

 <listbox cliptobounds="false" x:name="lbbestsellerimage" width="auto"    itemssource="{binding}" itemspanel="{dynamicresource iptlistbox}"   itemcontainerstyle="{dynamicresource listboxitemstyle}" />   <style x:key="listboxitemstyle" targettype="{x:type listboxitem}">         <setter property="background" value="transparent"/>         <setter property="padding" value="2,0,0,0"/>         <setter property="template">             <setter.value>                 <controltemplate targettype="{x:type listboxitem}">                     <grid width="150">                         <grid.rowdefinitions>                             <rowdefinition/>                             <rowdefinition/>                         </grid.rowdefinitions>                         <image horizontalalignment="center" verticalalignment="center" grid.row="0" x:name="img" source="{binding imageurl}" height="74" stretch="fill" width="75"/>                                                         </grid>                 </controltemplate>             </setter.value>         </setter>     </style> 

the easiest create new value converter gets passed in uri , gives bitmapimage... can decided if uri available or not... if available, load uri bitmapimage, else load default bitmapimage!

public class imageconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         string uri = (string)value;         if (string.isnullorempty(uri))             return new bitmapimage(new uri(uritomydefaultimage));          return new bitmapimage(new uri(uri));      }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

the following value converter expects string represents uri image... if null or empty, default image showed, else loads image!


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -