WPF Strange problem with dynamically binding items to a new ListBox -


here's code. is, creates new listbox control inside stackpanel , adds new item. problem described @ bottom of page.

mywindow.xaml

<window.resources>     <datatemplate x:key="itemstemplate">         <border width="200" height="50" >             <listbox itemssource="{binding title}" />         </border>     </datatemplate> </window.resources>  <grid>     <grid.columndefinitions>         <columndefinition width="100"/>         <columndefinition width="*"/>     </grid.columndefinitions>      <itemscontrol grid.column="1"                   grid.row="0"                   itemssource="{binding elementname=mainwindow, path=dataitems}"                   itemtemplate="{staticresource itemstemplate}">         <itemscontrol.template>             <controltemplate>                 <scrollviewer horizontalscrollbarvisibility="auto">                     <itemspresenter />                 </scrollviewer>             </controltemplate>         </itemscontrol.template>         <itemscontrol.itemspanel>             <itemspaneltemplate>                 <stackpanel orientation="horizontal" />             </itemspaneltemplate>         </itemscontrol.itemspanel>     </itemscontrol> </grid> 

mywindow.xaml.cs

    private observablecollection<mydataitem> dataitems;      public observablecollection<mydataitem> dataitems     {         { return dataitems; }     }      public window1()     {         dataitems = new observablecollection<mydataitem>();         initializecomponent();     } 

mydataitem.cs

public class mydataitem : dependencyobject {     public string title     {                 {             return (string)getvalue(titleproperty);         }         set         {             setvalue(titleproperty, value);         }     }      public static readonly dependencyproperty titleproperty =         dependencyproperty.register("title", typeof(string),                                      typeof(mydataitem), new uipropertymetadata(""));      public mydataitem(string title)     {         title = title;     } } 

add new listbox item

    void addnewitem_click(object sender, routedeventargs e)     {         mydataitem item = new mydataitem("12");         dataitems.add(item);         e.handled = true;     } 

after action, see new listbox control, item's characters treated separate items can see @ picture. why ?

the result

[ edit ]

solved, had change string list everywhere in class:

public class mydataitem : dependencyobject {     public list<string> title     {                 {             return (list<string>)getvalue(titleproperty);         }         set         {             setvalue(titleproperty, value);         }     }      public static readonly dependencyproperty titleproperty =         dependencyproperty.register("title", typeof(string),                                      typeof(mydataitem),                                      new uipropertymetadata(new list<string>()));      public mydataitem(list<string> title)     {         title = title;     } } 

because giving data source string.. converting string collection (character array) since itemssource property collection..

if still want create list box in data template single item (in case title).. try this..

change title property list , while creating title create list contains 1 string . item source list , since itemssource contains 1 string create 1 item.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -