c# - Problem in use of SortDescription in WPF - int and string are not IComparable? -


i have problem in using sortdescription. i've found thread problem, if want sort type doesn't implement icomparable, user defined class, it's not case.

i have class, has 2 properties: string id, , int value. let's call item! , have view:

<usercontrol> <!-- ... -->     <grid>         <grid.rowdefinitions>             <rowdefinition/>             <rowdefinition/>             <rowdefinition/>         </grid.rowdefinitions>          <button click="button_click"                 content="sort id"                 grid.row="0"/>         <button click="button_click1"                 content="sort value"                 grid.row="1"/>         <dockpanel grid.row="2">             <itemscontrol x:name="mitemscontrol"                           itemssource="{binding items}"><!-- type of items observablecollection<item> -->                 <!-- ... -->             </itemscontrol>         </dockpanel>     </grid> </groupbox> 

eventhandlers these:

private void button_click(object sender, routedeventargs e)     {         mitemscontrol.items.sortdescriptions.add(new sortdescription("id", listsortdirection.ascending); //exception here     } private void button_click1(object sender, routedeventargs e)     {         mitemscontrol.items.sortdescriptions.add(new sortdescription("value", listsortdirection.ascending); //...and here     } 

i invalidoperationexception because "failed compare 2 elements in array.", , because neither of elements implement icomparable. , is, can't understand, can compare ints, strings.

thanks idea!

this works fine me doing wrong in other parts of code. compare below sample.

xaml:

<window x:class="sortdemo.window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="window1" height="300" width="300">     <stackpanel>         <button click="onsort" content="sort id" tag="id"/>         <button click="onsort" content="sort value" tag="value"/>         <itemscontrol name="_itemscontrol" itemssource="{binding path=items}" />     </stackpanel> </window> 

code behind:

using system; using system.collections.objectmodel; using system.componentmodel; using system.windows;  namespace sortdemo {     public partial class window1 : window     {         public window1()         {             initializecomponent();              items = new observablecollection<item>();             items.add(new item() { id = "aaa", value = 2 });             items.add(new item() { id = "bbb", value = 1 });              datacontext = this;         }          public observablecollection<item> items { get; private set; }          private void onsort(object sender, routedeventargs e)         {             string sortproperty = (sender frameworkelement).tag string;             _itemscontrol.items.sortdescriptions.clear();             _itemscontrol.items.sortdescriptions.add(new sortdescription(sortproperty, listsortdirection.ascending));          }     }      public class item     {         public string id { get; set;}         public int value { get; set; }          public override string tostring()         {             return id + " " + value;         }     } } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -