WPF ComboBox Data Binding Binding

Source: Internet
Author: User

ComboBox data-bound list lists a collection of values that differentiate between display values and selections

Overall effect:

Print selected values and display values separately according to the ComboBox selection

Code:

Windows Forms:

1 <Windowx:class= "Comboxbinding.mainwindow"2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"4 Title= "Combox binding"Height= "192.857"Width= "385"windowstartuplocation= "Centerscreen"Loaded= "window_loaded">5     <Grid>6         <ComboBoxName= "ComBox1"HorizontalAlignment= "Left"Margin= "74,10,0,0"Width= "209"Height= "All"VerticalAlignment= "Top"/>7         <TextBlockName= "Txtselectedvalue"Width= "$"Text="{Binding elementname=combox1, path=selectedvalue}"HorizontalAlignment= "Left"Margin= "115,58,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Background= "#FFE7FBFA"/>8         <TextBlockName= "Txtselectedtext"Width= "$"Text="{Binding elementname=combox1, path=text}"HorizontalAlignment= "Left"Margin= "114,88,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Background= "#FFE7FBFA"/>9         <LabelContent= "SelectedValue"HorizontalAlignment= "Left"Margin= "2,58,0,0"VerticalAlignment= "Top"/>Ten         <LabelContent= "SelectedText"HorizontalAlignment= "Left"Margin= "10,86,0,0"VerticalAlignment= "Top"/> One  A     </Grid> - </Window>
XamlForm CS Code
1 usingSystem.Collections.Generic;2 usingSystem.Windows;3 4 namespacecomboxbinding5 {6     /// <summary>7     ///the interactive logic of MainWindow.xaml8     /// </summary>9      Public Partial classMainwindow:windowTen     { One          PublicMainWindow () A         { - InitializeComponent (); -         } the  -         Private voidWindow_Loaded (Objectsender, RoutedEventArgs e) -         { -List<comboxbind> Lstcmbbind =NewList<comboxbind> ();//used to bind a data source +  -             //initializing the data source +Comboxbind CBB =NewComboxbind ("Display value 1","Select a value of 1"); A Lstcmbbind.add (CBB); atCBB =NewComboxbind ("Display value 2","Select a value of 2"); - Lstcmbbind.add (CBB); -CBB =NewComboxbind ("Display Value 3","Select a value of 3"); - Lstcmbbind.add (CBB); -  -              This. Combox1.itemssource =Lstcmbbind; inCombox1.displaymemberpath ="Cmbtext";//properties in the class Comboxbind -Combox1.selectedvaluepath ="Cmbvalue";//properties in the class Comboxbind to         } +     } -}
View Code

Class used to bind a ComboBox

1 namespacecomboxbinding2 {3     /// <summary>4     ///for Combox data binding5     /// </summary>6     classComboxbind7     {8         //constructor Function9          PublicComboxbind (string_cmbtext,string_cmbvalue)Ten         { One              This. Cmbtext =_cmbtext; A              This. Cmbvalue =_cmbvalue; -         } -  the         //used to display values -         Private stringCmbtext; -          Public stringCmbtext -         { +             Get{returnCmbtext;} -             Set{Cmbtext =value;} +         } A  at         //values for actual selection -         Private stringCmbvalue; -          Public stringCmbvalue -         { -             Get{returnCmbvalue;} -             Set{Cmbvalue =value;} in         } -     } to}
C #

WPF ComboBox Data Binding Binding

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.