wpf - Combobox binding in Datatemplate -


hi creating wpf datagrid custom control. want combobos should show genders in combobox , heppening when keeping combobox outside datatemplate working inside datatemplate not working. please me ?

<usercontrol x:class="custom_datagrid.grid3.grid3"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               mc:ignorable="d" xmlns:sdk="http://schemas.microsoft.com/wpf/2008/toolkit"              d:designheight="300" d:designwidth="300" >     <usercontrol.resources>          <datatemplate x:key="duedatecelltemplate">             <textblock text="{binding dob}" margin="5,4,5,4"/>         </datatemplate>         <datatemplate x:key="duedatecelleditingtemplate">             <datepicker selecteddate="{binding dob, mode=twoway}"/>         </datatemplate>          <datatemplate x:key="gendercelltemplate">             <textblock text="{binding gender.gender}" margin="5,4,5,4"/>         </datatemplate>         <datatemplate x:key="gendercelleditingtemplate">             <!--<combobox itemssource="{binding genders}" selecteditem="{binding genders}" selectedvalue="{binding path=gender, elementname=id}" displaymemberpath="id" selectedvaluepath="gender"></combobox>-->             <combobox x:name="c1" itemssource="{binding genders}" displaymemberpath="id"  height="50" width="100"></combobox>             <!--<textblock text="{binding genders.gender}" foreground="khaki"></textblock>-->         </datatemplate>      </usercontrol.resources>     <grid>         <datagrid x:name="datagrid3" autogeneratingcolumn="datagrid3_autogeneratingcolumn" itemssource="{binding employees}" foreground="steelblue"></datagrid>      </grid> </usercontrol> 
public enum designation     {         sales, development, hr, backoffice     }  public class sex     {         private string id;         private string gen;          public string id         {                         {                 return id;             }             set             {                 id = value;             }         }          public string gender         {                         {                 return gen;             }             set             {                 gen = value;             }         }      }  public class datasource     {         observablecollection<employee> emplist = new observablecollection<employee>();         list<sex> genders = new list<sex>();            public datasource()         {              emplist.add(new employee() { id = 1, name = "neeraj", dob = convert.todatetime("12/03/1986"), email = "neeraj@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = false, designation = designation.development });             emplist.add(new employee() { id = 2, name = "mayank", dob = convert.todatetime("01/01/1986"), email = "mayank@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = true, designation = designation.backoffice });             emplist.add(new employee() { id = 1, name = "neeraj", dob = convert.todatetime("12/03/1986"), email = "neeraj@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = false, designation = designation.development });             emplist.add(new employee() { id = 2, name = "mayank", dob = convert.todatetime("01/01/1986"), email = "mayank@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = true, designation = designation.backoffice });             emplist.add(new employee() { id = 1, name = "neeraj", dob = convert.todatetime("12/03/1986"), email = "neeraj@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = false, designation = designation.development });             emplist.add(new employee() { id = 2, name = "mayank", dob = convert.todatetime("01/01/1986"), email = "mayank@mail.com", gender = new sex() { id = "m", gender = "male" }, phone = "9999999999", active = true, designation = designation.backoffice });              genders.add(new sex() { id = "m", gender = "male" });             genders.add(new sex() { id = "f", gender = "female" });           }             public observablecollection<employee> employees         {                         {                 return emplist;             }             set             {                 emplist = value;             }         }          public list<sex> genders         {                         {                 return genders;             }             set             {                 genders = value;             }         }       } 

coding of xaml.cs file

using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes;     namespace custom_datagrid.grid3 {     /// <summary>     /// interaction logic grid3.xaml     /// </summary>     public partial class grid3 : usercontrol     {         public grid3()         {             initializecomponent();             this.datacontext = new datasource();         }          private void datagrid3_autogeneratingcolumn(object sender, datagridautogeneratingcolumneventargs e)         {             if (e.propertyname.tolower().tostring().equals("id"))             {                 e.column.header = "employee id";             }             else if (e.propertyname.tolower().equals("name"))             {                 e.column.header = "employee name";             }             else if (e.propertyname.tolower().equals("dob"))             {                 e.column.header = "employee dob";                 if (e.propertytype == typeof(datetime))                 {                     datagridtemplatecolumn templatecolumn = new datagridtemplatecolumn();                     templatecolumn.header = "employee dob";                     templatecolumn.celltemplate = (datatemplate)resources["duedatecelltemplate"];                     templatecolumn.celleditingtemplate = (datatemplate)resources["duedatecelleditingtemplate"];                     templatecolumn.sortmemberpath = "duedate";                     e.column = templatecolumn;                 }             }             else if (e.propertyname.tolower().equals("phone"))             {                 e.column.header = "employee phone";                 e.cancel = true;             }             else if (e.propertyname.tolower().equals("email"))             {                 e.column.header = "employee email";             }             else if (e.propertyname.tolower().equals("gender"))             {                 e.column.header = "employee gender";                 datagridtemplatecolumn templatecolumn = new datagridtemplatecolumn();                 templatecolumn.header = "employee gender";                 templatecolumn.celltemplate = (datatemplate)resources["gendercelltemplate"];                 templatecolumn.celleditingtemplate = (datatemplate)resources["gendercelleditingtemplate"];                 e.column = templatecolumn;             }             else if (e.propertyname.tolower().equals("active"))             {                 e.column.header = "employee active/inactive";             }             else if (e.propertyname.tolower().equals("designation"))             {                 e.column.header = "employee designation";             }         }        } } 

so trying create template column combobxo in celleditingtemplate every things working fine inside datatemplate not working. please me? check in text box have commented working fine...

if inside datatemplate datacontext object being templated. binding, relative datacontext if binding path specified not find itemssource.

normally can use relativesource-binding find control still has datacontext in itemssource can found. (see rv1987's answer; thought did not work earlier because if have datagridcomboboxcolumn same relativesource-itemssource-binding not work, because column on own abstract , not appear in trees unlike control created template)

since datacontext of usercontrol should looking can name usercontrol (control example) , bind this:

itemssource="{binding source={x:reference control}, path=datacontext.genders}" 

(note x:reference quite new, not exist in .net 3.5, using elementname=control instead not work)


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -