c# - Binding images in WPF -
i want show multiple image on uniform grid i'm getting urls internet).when execute code images not displayed. videos window:
<window x:name="videos" x:class="navigateur.presentation.videos" xmlns:my="clr-namespace:navigateur.presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="window2" height="207" width="463" windowstyle="none" resizemode="noresize" topmost="true" windowstate="maximized"> <grid> <itemscontrol margin="72,30,76,30" itemssource="{binding images}"> <itemscontrol.itemspanel> <itemspaneltemplate> <uniformgrid columns="4" rows="3"/> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <image source="{binding}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid> </window>
and code behind:
using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.shapes; namespace navigateur.presentation { /// <summary> /// logique d'interaction pour window2.xaml /// </summary> public partial class videos : window { observablecollection<image> images; public videos() { initializecomponent(); } public observablecollection<image> images() { images = new observablecollection<image>(); servicereferencevideo.videoserviceclient wcf = new servicereferencevideo.videoserviceclient(); foreach (var item in wcf.getallvideos()) { string link_thumb = wcf.getthumbimage((wcf.getvideoid(item.urlvideo))); var wsource = new bitmapimage(new uri(link_thumb)); var wimage = new image { source = wsource }; images.add(wimage); } return images; } } }
quite few issues here:
- your list (
itemscontrol
) accesses images viabinding
, never set list'sdatacontext
- object binding system try data (in case,videos
window itself). - bindings work on public properties, not private fields
images
collection. - when
images()
function done loading thumbnails, binding system needs notified bound collection (public property) has changed, either implementinginotifypropertychanged
, raisingpropertychanged
event, or making propertydependencyproperty
. - the way it's written now,
images()
function creates collection ofimage
s, , itemscontrol uses them sources newimage
s. usingimage
source ofimage
overkill (if works).image
happliy takes e.g. uri source though, instead of generatingimage
s in code,images
collection can list oflink_thumb
urls.
i suggest read on data binding in wpf. there lots of resources out there.
for particular case though, because done in videos
window, skip bindings altogether, , force-feed list of images itemscontrol
if give name:
<itemscontrol margin="72,30,76,30" x:name="_imagelist" > ...
code-behind:
public void images() { var images = new observablecollection<string>(); var wcf = new servicereferencevideo.videoserviceclient(); foreach (var item in wcf.getallvideos()) { string link_thumb = wcf.getthumbimage((wcf.getvideoid(item.urlvideo))); images.add(link_thumb); } _imagelist.itemssource = images; }
Comments
Post a Comment