Strange behaviour when binding to ListBox in WPF -


i noticed strange behaviour when binding array listbox. when add items same "name", can't select them in runtime - listbox goes crazy. if give them unique "names", works fine. please explain why happening?

the view:

<window x:class="listboxtest.listboxtestview"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:listboxtest"         title="listboxtestview" height="300" width="300">     <window.resources>         <local:listboxtestviewmodel x:key="model" />     </window.resources>     <grid datacontext="{staticresource resourcekey=model}">         <listbox itemssource="{binding items}" margin="0,0,0,70" />         <button command="{binding path=add}"  content="add" margin="74,208,78,24" />     </grid> </window> 

the view model:

using system.collections.generic; using system.componentmodel; using system.windows.input;  namespace listboxtest {     internal class listboxtestviewmodel : inotifypropertychanged     {         private list<string> realitems = new list<string>();          public listboxtestviewmodel()         {             realitems.add("item a");             realitems.add("item b");         }          public event propertychangedeventhandler propertychanged;         private void onpropertychanged(string propertyname)         {             if (propertychanged != null)                 propertychanged(this, new propertychangedeventargs(propertyname));         }          public string[] items         {             { return realitems.toarray(); }         }          public icommand add         {             // delegatecommand prism             { return new delegatecommand(doadd); }         }          private int x = 1;         public void doadd()         {             var newitem = "item";             // uncomment fix             //newitem += " " + (x++).tostring();             realitems.add(newitem);             onpropertychanged("items");         }     } } 

all items in wpf listbox must unique instances. strings have same constant value not unique instances due string interning. around this, need encapsulate item in more meaningful object string, such as:

public class dataitem  {      public string text { get; set; }  } 

now can instantiate multiple dataitem instances , create itemdatatemplate render text textblock. can override dataitem tostring() if want use default rendering. can have multiple dataitem instances same text , no problems.

this limitation may seem bit strange, simplifies logic, selecteditem has one-to-one correspondence selectedindex items in list. in line wpf approach data visualization, tends toward lists of meaningful objects opposed lists of plain strings.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -