c# - Migrating from `ImageList` in WinForms to WPF -
i'd move project wpf , in have imagelist
(loaded selecting 25 images in design view) utilize create buttons follows: i've simplified project downwards next code. this whole project (aside autogenerated code in .designer.cs):
public partial class form1 : form { button[] buttonlist = new button[25]; size buttonsize = new size(140, 140); public form1() { initializecomponent(); this.clientsize = new size(142 * 5, 142 * 5); (int = 0; < buttonimages.images.count; i++) { buttonlist[i] = new button(); buttonlist[i].size = buttonsize; buttonlist[i].location = new point( (i % 5) * (buttonsize.width + 2) + 1, (i / 5) * (buttonsize.height + 2) + 1); buttonlist[i].image = buttonimages.images[i]; } suspendlayout(); controls.addrange(buttonlist); resumelayout(false); } }
i can't seem wrap head around how trivial task in wpf. best can tell answers on here (like this) should be
filling folder images creating resourcedictionary referencing them creating file references resourcedictionary reference create else (i can't figure out what) accesses images through resourcedictionary load them buttons (but button class doesn't have constructor...???!!!)can help translate wpf? honestly, can't figure out start.
if matters, here's looks when runs:
we shall utilize mvvm this.
first create model.
public class mymodel { public bitmapsource image { get; set; } public string description { get; set; } }
then our viewmodel
public class myviewmodel { public observablecollection<mymodel> images { get; set; } public icommand buttonclicked { get; set; } ... logic populate images }
and our view
<window x:class="testwpf.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="clr-namespace:testwpf" title="mainwindow" height="350" width="525"> <window.resources> <loc:myviewmodel x:key="viewmodel" /> </window.resources> <grid datacontext="{staticresource viewmodel}"> <listview itemssource="{binding images}"> <listview.itemtemplate> <datatemplate> <button command="{binding buttonclicked, relativesource=parent}" commandparameter="{binding description}"> <image width="50" height="50" source="{binding picture}"></image> </button> </datatemplate> </listview.itemtemplate> </listview> </grid> </window>
that create list should deed in same way.
c# wpf winforms imagelist
No comments:
Post a Comment