Bind list of images to StackPanel -
i want bind list of images stackpanel inside datagrid.rowdetailstemplate. class structure follows:
public class { private list<myimage> _images = new list<myimage>(); public list<myimage> images { get; set; } public string name { get; set; } public void addimage(byte[] src) { ... } } public class myimage { public bitmapimage image { get; set; } public byte[] rawdata { get; set; } }
in main class have list of a:
public list<a> alist { get; set; } datagrid1.itemssource = alist; datagrid1.datacontext = alist;
all want display name property of element in datagridtextcolumn , images stored in images property in rowdetails.
my xaml is:
<datagrid name="datagrid1"> <datagrid.columns> <datagridtextcolumn header="name" binding="{path=name}"/> </datagrid.columns> <datagrid.rowdetailstemplate> <datatemplate> <stackpanel datacontext="{binding path=images}"> <image source="{binding path=rawdata}"/> </stackpanel> </datatemplate> </datagrid.rowdetailstemplate>
all see 1 image although there more stored in images. ideas?
ok, solution of problem use of contentpresenter combined converter.
now xaml looks this:
<datagrid name="datagrid1"> <datagrid.columns> <datagridtextcolumn header="name" binding="{path=name}"/> </datagrid.columns> <datagrid.rowdetailstemplate> <datatemplate> <contentpresenter content="{binding images, converter={staticresource imagecollectionconverter}}"/> </datatemplate> </datagrid.rowdetailstemplate> </datagrid>
and corresponding converter class:
public class imagecollectionconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { list<myimage> images = value list<myimage>; if (images != null) { stackpanel stack = new stackpanel(); stack.orientation = orientation.horizontal; foreach (designimage img in images) { image image = new image(); image.source = img.image; stack.children.add(image); } return stack; } return null; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }
Comments
Post a Comment