c# - WPF Binding DataGrid -


i trying bound properties datacontext datagrid without success... datagridcombobox empty , invisible before clicking on it, , have 2 rows instead of 1 following source code.

the xaml

<datagrid grid.column="0" grid.row="2" grid.columnspan="2" itemssource="{binding classrow}" autogeneratecolumns="false">   <datagrid.columns>     <datagridcomboboxcolumn header="classe" x:name="class_classname" width="100" selecteditembinding="{binding classname, mode=twoway}" itemssource="{binding classlist}"/>     <datagridtemplatecolumn header="niveau">       <datagridtemplatecolumn.celltemplate>         <datatemplate>           <stackpanel orientation="horizontal">             <textbox x:name="leveltxt"  width="50" textchanged="leveltxttextchanged" text="{binding classlevel, targetnullvalue={x:static sys:string.empty}}"/>             <stackpanel orientation="vertical">               <button x:name="levelup"   content="+" width="15" height="15" click="levelupclick" fontsize="10" verticalcontentalignment="top" />               <button x:name="leveldown" content="-" width="15" height="15" click="leveldownclick" fontsize="12" verticalcontentalignment="bottom"/>             </stackpanel>           </stackpanel>         </datatemplate>       </datagridtemplatecolumn.celltemplate>     </datagridtemplatecolumn>   </datagrid.columns> </datagrid> 

the datacontext :

public class classrow {     public string classname;     public int    classlevel;   }  public class pjdatawindow : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;     protected list<classrow>  m_classrow;     protected list<string>    m_classlist;      public pjdatawindow()     {         m_classrow   = new list<classrow>();         m_classlist  = new list<string>();          //test         m_classlist.add("classe1");         m_classrow.add(new classrow { classname = "classe1", classlevel = 2 });          onpropertychanged("classlist");         onpropertychanged("classrow");     }      protected void onpropertychanged(string name)     {         propertychanged?.invoke(this, new propertychangedeventargs(name));     }      public list<classrow>  classrow     {                 {             return m_classrow;         }          set         {             m_classrow = value;             onpropertychanged("classrow");         }     }      public list<string> classlist { => m_classlist; set { m_classlist = value; onpropertychanged("classlist"); } } } 

i new in wpf , search has led me nowhere...

thank lot !

there 3 problems (so far).

the easiest problem 2 rows instead of one. row new item row. turn off so...

  <datagrid grid.column="0" grid.row="2" grid.columnspan="2" itemssource="{binding classrow}" autogeneratecolumns="false" margin="40" canuseraddrows="false"> 

the next problem empty text field. can bind properties not fields. fix change fields in row object properties.

public class classrow {     public string classname { get; set; }     public int classlevel { get; set; } } 

finally reason combo box empty items source not bound data context. data grid column outside visual tree. cannot find source. fix this.

    <frameworkelement x:name="dummyelement" visibility="collapsed"/>     <datagrid grid.column="0" grid.row="2" grid.columnspan="2" itemssource="{binding classrow}" autogeneratecolumns="false" margin="40" canuseraddrows="false" x:name="datagrid">         <datagrid.columns>             <datagridcomboboxcolumn header="classe" x:name="class_classname" width="100" selecteditembinding="{binding classname, mode=twoway}" itemssource="{binding source={x:reference dummyelement}, path=datacontext.classlist}"/> 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -