c# - How to bind the source of an image to a directory + name in wpf? -


i have list of image files want create datagrid based on them , show thumbnail. list contains images files related path such follow:

class myclass {     public list<string> images;     public string rootpath;  }  

i need write converter bind 2 parameter , create thumbnail , result became source of images.

i wrote converter create source of image follow:

    public object convert(object value, type targettype, object parameter, cultureinfo culture)     {          try         {             var bi = new bitmapimage();             bi.begininit();             bi.decodepixelwidth = 100;             bi.cacheoption = bitmapcacheoption.onload;             bi.urisource = new uri(value.tostring());             bi.endinit();             return bi;          }         catch         {            // nothing. maybe return default image         }         return null;     } 

but converter bind 1 property, need generate way bind 2 (or more) values? how can this?

you might use multi-value converter shown in following itemscontrol example. uses multibinding source property of image control, first binding uses datacontext of itemscontrol access rootpath property.

<itemscontrol x:name="itemscontrol" itemssource="{binding images}">     <itemscontrol.itemtemplate>         <datatemplate>             <image stretch="none">                 <image.source>                     <multibinding converter="{staticresource imagepathconverter}">                         <binding path="datacontext.rootpath"                                  elementname="itemscontrol"/>                         <binding/>                     </multibinding>                 </image.source>             </image>         </datatemplate>     </itemscontrol.itemtemplate> 

the example assumes view model class looks this:

public class viewmodel {     public list<string> images { get; set; }     public string rootpath { get; set; } } 

the converter implemented this:

public class imagepathconverter : imultivalueconverter {     public object convert(         object[] values, type targettype, object parameter, cultureinfo culture)     {         var path = path.combine(values.oftype<string>().toarray());         var bi = new bitmapimage();         bi.begininit();         bi.decodepixelwidth = 100;         bi.cacheoption = bitmapcacheoption.onload;         bi.urisource = new uri(path);         bi.endinit();         return bi;     }      public object[] convertback(         object value, type[] targettypes, object parameter, cultureinfo culture)     {         throw new notsupportedexception();     } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -