c# - Sort manually added controls inside ListBox -


i have listbox:

<listbox name="lbfrequentcolumnitems" grid.row="1" minheight="0"></listbox> 

i adding many imagebuttons above listbox shown below:

imagebutton b = new imagebutton(); b.content = d.displayname;               b.click += new routedeventhandler(optionalcolumnitems_click); lbfrequentcolumnitems.items.add(b); 

on button click need show imagebuttons in sorted order content.

i can copying in list , sorting , again adding buttons.

but there direct way or method on listbox perform it?

i tried below, not working don't have property bindings:

lbfrequentcolumnitems     .items     .sortdescriptions     .add(          new system.componentmodel.sortdescription("",             system.componentmodel.listsortdirection.ascending)); 

you can sort them content property:

private void button_click(object sender, routedeventargs e) {     lbfrequentcolumnitems         .items         .sortdescriptions         .add(new sortdescription("content", listsortdirection.ascending)); } 

it won't throw invalidoperationexception because system.string implements icomparable interface, not implemented (image)button.

demo:

public mainwindow() {     initializecomponent();      var names = enumerable         .range(1, 10)         .orderby(_ => guid.newguid())         .select(i =>             i.tostring());      foreach (var button in this.createnewbuttons(names))     {         lbfrequentcolumnitems.items.add(button);                     } }  private ienumerable<button> createnewbuttons(ienumerable<string> names) {     foreach (var name in names)     {         button b = new button();         b.content = name;         b.click += new routedeventhandler(optionalcolumnitems_click);          yield return b;     } }  private void optionalcolumnitems_click(object sender, routedeventargs e) {     throw new notimplementedexception(); } 

xaml:

    <grid.rowdefinitions>         <rowdefinition/>         <rowdefinition/>     </grid.rowdefinitions>     <listbox name="lbfrequentcolumnitems" grid.row="0" minheight="0"></listbox>     <button grid.row="1" content="reorder" click="button_click"/> 

p.s.: in same way can set button datacontext property specific dataobject implementing icomparable , sort datacontext - new sortdescription("datacontext", listsortdirection.ascending)

p.s.1: while manually adding buttons not prohibited, nonetheless better use advanced databinding , templating capabilites of wpf. they, after initial investment, make application easier develop , modify.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -