c# - Second-level property binding in wpf Datagrid DataGridTemplateColumn TextBlock -


i have got datagrid columns created of datagridtemplatecolumn technology. here column interested in in datagrid (name = "dgrv)


xaml


<datagridtemplatecolumn width="40">                     <!--="name"-->                     <datagridtemplatecolumn.celltemplate>                         <datatemplate>                             <stackpanel>                                 <textblock text="{binding textcell.txtbx}"                                             textalignment="center" margin="0, 8, 0, 0"/>                                 <canvas background="black" height="1" margin="5,5,5,0"/>                                 <textblock text="{binding operatorcode}" textalignment="center" margin="0, 5, 0, 0"/>                             </stackpanel>                         </datatemplate>                     </datagridtemplatecolumn.celltemplate>                 </datagridtemplatecolumn> 

in backend have got next code:


xaml.cs


var list = new list<contract>();         list.add(new contract         {             id = 1,             companyname = "Технабсервис",             headname = "Петров\nАлександр\nНиколаевич",             countrycode = "+7",             operatorcode = "495",             date = convert.todatetime("21.02.2012").tostring("dd.mm"), //getdatetimeformats('mm, dd'),              time = "16:39",             telephones = "(495)123-4567 (общий)",             comment = ""         }); dgrv.itemssource = list; 

here class contract.cs


public class contract     {         public int id { get; set; }         public string companyname { get; set; }         public string headname { get; set; }         public string countrycode { get; set; }         public string operatorcode { get; set; }         public string date { get; set; }         public string time { get; set; }         public string telephones { get; set; }         public string comment { get; set; }         public textcell smth { get; set; }          /*public dictionary<string, int> pricefields { get; private set; }         public dictionary<string, string> ratefields { get; private set; }         public dictionary<string, string> typefields { get; private set; }         public dictionary<string, string> ticketfields { get; private set; }*/          public contract()         {             smth = new textcell();         }     } 

and, class textcell (as see, want bind textblock`s text field of class name "txtbx"


textcell.cs


public class textcell     {         public string txtbx { get; set; }         public textcell()         {             txtbx = "sdbjshfk";         }          public override string tostring()         {             return txtbx;         }     } 

so, question part of code

<textblock text="{binding textcell.txtbx}"                                                 textalignment="center" margin="0, 8, 0, 0"/> 

it not working @ all. understand, can bind textblock`s text textcell field itself. tostring() method called in such case, not enough me. mainly, because situation described in post example , have got far more situations need such type of binding.

so, textcell class has been created example in order understand required technology.

the answer pretty simple.

if want bind subproperty have use properties names in binding markup extension.

when write {binding textcell.txtbx}, of course it's not going work, because don't have property called textcell on contract. have property of type textcell called smth. c# code try reach property using following code:

 var list = new list(); list.add(new contract {     id = 1,     companyname = "Технабсервис",     headname = "Петров\nАлександр\nНиколаевич",     countrycode = "+7",     operatorcode = "495",     date = convert.todatetime("21.02.2012").tostring("dd.mm"), //getdatetimeformats('mm, dd'),      time = "16:39",     telephones = "(495)123-4567 (общий)",     comment = "" });  var x = list[0].smth.txtbx; 

by way should consistent naming conventions. should use pascal casing public properties.

you can read more syntax of binding markup extension here.

there still problem code. if update fields code wouldn't update ui. in order make list[0].smth.txtbx = "updated text"; work should implement inotifypropertychanged interface on contract , textcell classes. along these lines:

public class contract : inotifypropertychanged {     private int _id;     private string _companyname;     private string _headname;     private string _countrycode;     private string _operatorcode;     private string _date;     private string _time;     private string _telephones;     private string _comment;     private textcell _smth;      public int id     {         { return _id; }         set         {             _id = value;             onpropertychanged();         }     }      public string companyname     {         { return _companyname; }         set         {             _companyname = value;             onpropertychanged();         }     }      public string headname     {         { return _headname; }         set         {             _headname = value;             onpropertychanged();         }     }      public string countrycode     {         { return _countrycode; }         set         {             _countrycode = value;             onpropertychanged();         }     }      public string operatorcode     {         { return _operatorcode; }         set         {             _operatorcode = value;             onpropertychanged();         }     }      public string date     {         { return _date; }         set         {             _date = value;             onpropertychanged();         }     }      public string time     {         { return _time; }         set         {             _time = value;             onpropertychanged();         }     }      public string telephones     {         { return _telephones; }         set         {             _telephones = value;             onpropertychanged();         }     }      public string comment     {         { return _comment; }         set         {             _comment = value;             onpropertychanged();         }     }      public textcell smth     {         { return _smth; }         set         {             _smth = value;             onpropertychanged();         }     }      /*public dictionary<string, int> pricefields { get; private set; }     public dictionary<string, string> ratefields { get; private set; }     public dictionary<string, string> typefields { get; private set; }     public dictionary<string, string> ticketfields { get; private set; }*/      public contract()     {         smth = new textcell();     }      public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged([callermembername] string propertyname = null)     {         var handler = propertychanged;         if (handler != null) handler(this, new propertychangedeventargs(propertyname));     } } 

and textcell class:

public class textcell : inotifypropertychanged {     private string _txtbx;      public string txtbx     {         { return _txtbx; }         set         {             _txtbx = value;             onpropertychanged();         }     }      public textcell()     {         txtbx = "sdbjshfk";     }      public override string tostring()     {         return txtbx;     }      public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged([callermembername] string propertyname = null)     {         var handler = propertychanged;         if (handler != null) handler(this, new propertychangedeventargs(propertyname));     } } 

i know lot of boilerplate code, unfortunately need if want binding notified changes coming viewmodel/model. there library called propertychanged fody lets avoid writing boilerplate code inotifypropertychanged implementations. i'm getting sidetracked.

i hope was/will useful.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -