c# - How to make a list of buttons dynamically and show them in the MainForm in a ListBox or ItemsControl -


i new in wpf. want make list of buttons list of class "buttons" , having 2 fields (buttoncontent,buttonid) in mainform in wpf. idea behind when mainform loaded want make buttons list dynamically. best example given in link http://www.codeproject.com/articles/25030/animating-interactive-d-elements-in-a-d-panel want make equal size buttons stacked horizontally.

how can in wpf? thank in advance.

this way it. there areas here need further research on part, started.

first need viewmodel. plain old object. exposes properties , methods pertinent business. mentioned buttoncontent , buttonid. let's assume both of strings now. you'll need command button assume.

public class buttonviewmodel : inotifypropertychanged {    private string _content;    public string content    {       get{ return _content; }       set{ _content = value; onpropertychanged("content"); }    }     // you'll need implement inotifypropertychanged    // take @ relaycommand     // here command you're button    public icommand buttoncommand    {       { return new relaycommand(execute, canexecute); }    }     private void execute()    {      // actual command code    }     private bool canexecute()    {       // automatically toggle enabled state on button    } } 

you'll have 1 more view model. datacontext of mainwindow.

public class appviewmodel {    public observablecollection<buttonviewmodel> mybuttons {get; set; }     public appviewmodel()    {       mybuttons = new observablecollection<buttonviewmodel>();        // add demo data       mybuttons.add(new buttonviewmodel() {buttoncontent = "click me!"});      } } 

now need implement view. in mainwindow xaml code. add xmlns:local namespace.

<window.datacontext>    <local:appviewmodel /> </window.datacontext>  <listbox itemssource="{binding mybuttons}">    <listbox.itemstemplate>       <datatemplate>       <button command="{binding buttoncommand}" content="{binding content}"/>       </datatemplate>    <listbox.itemstemplate> </listbox> 

hopefully can started in right direction.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -