Various usage of ComboBox in Silverlight (basically summarize some methods above)

Source: Internet
Author: User

The front end contains several ComboBox controls.

 <  Grid  X: Name  = "Layoutroot"  Background  = "White"  >          <  ComboBox  Height  = "23"  Horizontalalignment  = "Left"  Margin  ="  Name = "Combox1"  Verticalalignment  = "TOP"  Width  = "120"   />          <  ComboBox  Height  = "23"  Horizontalalignment  = "Left"  Margin  = "223,49, 0, 0"  Name  = "Combox2" Verticalalignment  = "TOP"  Width  = "120"  Selectionchanged  = "Combobox2_selectionchanged"   />          <  ComboBox  Height  = "23"  Horizontalalignment  = "Left"  Margin  = "26,140"  Name = "Combox3"  Verticalalignment  = "TOP"  Width  = "120"   />          <  ComboBox  Height  = "23"  Horizontalalignment  = "Left"  Margin  = "223,140"  Name  = "Combox4" Verticalalignment  = "TOP"  Width  = "120"   />          <  ComboBox  Height  = "23"  Horizontalalignment  = "Left"  Margin  = "26,199"  Name  = "Combobox5"  Verticalalignment = "TOP"  Width  = "120"  Selectionchanged  = "Combobox5_selectionchanged"   />      </  Grid  > 

The backend CS file is as follows:

 Public   Partial   Class  Mainpage: usercontrol {  Public Mainpage () {initializecomponent (); initlizepage ();}  Public   Void  Initlizepage (){  # Region The most basic binding of combox1 Comboboxitem LBI = New  Comboboxitem (); LBI. setvalue (comboboxitem. contentproperty,  "  Jiangsu  "  );  This . Combobox1.items. Add ( "  Wuhan  "  );  This . Combobox1.items. Add ( "  Dalian  "  );  This . Combobox1.items. Add ( "  Suzhou  "  );  This . Combobox1.items. Add (LBI );  This . Combobox1.selecteditem = LBI;  # Endregion              # Region Built a data source List <Product> _ list = New List <product> (); _ List. Add (  New Product () {id = 11 , Name = "  Product 1  " }); _ List. Add (  New Product () {id = 22 , Name = "  Product 2  "  }); _ List. Add (  New Product () {id = 33 , Name = "  Product 3  "  }); _ List. Add (  New Product () {id =44 , Name = "  Product 4  "  }); _ List. Add (  New Product () {id = 55 , Name = "  Product 5  "  }); _ List. Add (  New Product () {id = 66 , Name = "  Product 6 "  });  # Endregion              # Region Add Data Source This . Combobox2.displaymemberpath = "  Name  "  ;  //  This. combobox2.selectedvaluepath = "ID ";  //  No value specified              This  . Combobox2.updatelayout (); This . Combobox2.itemssource = _ List;  //  Initialize and assign values. Data initialization using selecteditem ComboBox is different from dropdownlist on the web.              This . Combobox2.selecteditem = ( From P In   This  . Combobox2.items  Where (P As Product). Name = "  Product 4 "                                             Select  P). First ();  # Endregion                          # Region               This . Combobox5.displaymemberpath = "  Name  "  ;  This . Combobox5.selectedvaluepath = "  ID  " ; // Value              This  . Combobox5.updatelayout ();  This . Combobox5.itemssource = _ List;  Int Selectedindex =- 1  ;  For ( Int I = 0 ; I <_ list. Count; I ++ ){  If (_ List [I]. ID =33  ) {Selectedindex = I;  Break  ;}}  //  Use selectedindex to initiate binding              This . Combobox5.selectedindex = Selectedindex;  # Endregion               # Region Add custom item Comboboxitem cbiright =New  Comboboxitem (); cbiright. Background = New  Solidcolorbrush (colors. Yellow); cbiright. horizontalcontentalignment = Horizontalalignment. Right; cbiright. setvalue (comboboxitem. contentproperty,  "  Shanghai  "  ); Comboboxitem cbicenter = New  Comboboxitem (); cbicenter. Background =New  Solidcolorbrush (colors. Cyan); cbicenter. horizontalcontentalignment = Horizontalalignment. Center; cbicenter. setvalue (comboboxitem. contentproperty,  "  Beijing  "  ); Comboboxitem cbileft = New  Comboboxitem (); cbileft. Background = New  Solidcolorbrush (colors. lightgray); cbileft. horizontalcontentalignment =Horizontalalignment. Left; cbileft. setvalue (comboboxitem. contentproperty,  "  Shenzhen  "  );  This  . Combobox3.items. Add (cbiright );  This  . Combobox3.items. Add (cbicenter );  This  . Combobox3.items. Add (cbileft );  # Endregion  Image img =New  Image (); IMG. Source = New Bitmapimage ( New Uri ( "  IMG/1.png  "  , Urikind. Relative); label LBL = New  Label (); LBL. Content = "  Options with images  "  ; Stackpanel sp =New  Stackpanel (); sp. Orientation = Orientation. Horizontal; sp. Children. Add (IMG); sp. Children. Add (LBL); comboboxitem multiplecmb = New  Comboboxitem (); multiplecmb. Content = SP;  This  . Combobox4.items. Add (multiplecmb );}  Private   Void Combobox2_selectionchanged ( Object Sender, selectionchangedeventargs e) {product Product = (Sender As ComboBox). selecteditem As  Product;  Int Selectid = Product. ID;  String Selectedname = Product. Name;  //  MessageBox. Show (selectid. tostring () + selectedname );  Product product2 = Combobox2.selectedvalueAs  Product;  Int Selectedid2 = Product2.id;  String Selectedname2 = Product2.name;  //  MessageBox. Show (selectedid2.tostring () + selectedname2 );  }  //          Private   Void Combobox5_selectionchanged ( Object Sender, selectionchangedeventargs e ){  //  MessageBox. Show (ComboBox) sender). selectedvalue. tostring ());  //  Get value  //  MessageBox. Show (product) (ComboBox) sender). selecteditem). Name );  //  Get name ????  }} 

Of course, we also need to build a product class.

Public ClassProduct {Public IntId {Get;Set;}Public StringName {Get;Set;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.