c# - How do I bind my data to my combobox control wpf? -


this data wamt bind combobox control

public partial class mainwindow : window {     contact contact = new contact();       public mainwindow()     {         contact contact = new contact();         contact.titles.add(new title { title = "mr" });         contact.titles.add(new title { title = "dr " });         contact.titles.add(new title { title = "mis" });         contact.titles.add(new title { title = "miss" });         contact.titles.add(new title { title = "sir" });          contact.genders.add(new gender { gender = "male" });         contact.genders.add(new gender { gender = "female" });          datacontext = contact.genders;         datacontext = contact.titles;      } } 

you don't need set datacontext property twice, cuz it's property of mainwindow class. first make contact object datacontext mainwindow, that's all. implement inotifypropertychanged it.

and make bindings in xaml this:

<window x:class="wpfapp1.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:wpfapp1"         mc:ignorable="d"         title="mainwindow" height="350" width="525">     <grid>         <grid.columndefinitions>             <columndefinition/>             <columndefinition/>         </grid.columndefinitions>         <combobox itemssource="{binding path=titles}" maxheight="20"/>         <combobox itemssource="{binding path=genders}" grid.column="1"  maxheight="20"/>     </grid> </window> 

and there's mainwindows.xaml.cs

:   public partial class mainwindow : window     {         public contact contact;         public mainwindow()         {             initializecomponent();             contact = new contact();             this.datacontext = contact;              contact.titles.add(new title { title = "mr" });             contact.titles.add(new title { title = "dr " });             contact.titles.add(new title { title = "mis" });             contact.titles.add(new title { title = "miss" });             contact.titles.add(new title { title = "sir" });              contact.genders.add(new gender { gender = "male" });             contact.genders.add(new gender { gender = "female" });         }     }      public class contact : inotifypropertychanged     {         public observablecollection<title> titles         {             { return _titles; }             set             {                 _titles = value;                 notifypropertychanged(nameof(titles));             }         }          public observablecollection<gender> genders         {             { return _genders; }             set             {                 _genders = value;                 notifypropertychanged(nameof(genders));             }         }          private observablecollection<title> _titles { get; set; }         private observablecollection<gender> _genders { get; set; }           public contact()         {             titles = new observablecollection<title>();             genders = new observablecollection<gender>();         }          public event propertychangedeventhandler propertychanged;          private void notifypropertychanged(string info)         {             if (propertychanged != null)             {                 propertychanged(this, new propertychangedeventargs(info));             }         }     }      public class title     {         public string title { get; set; }         public override string tostring()         {             return title;         }     }      public class gender     {         public string gender { get; set; }         public override string tostring()         {             return gender;         }     } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -