In Silverlight, how does one obtain the ComboBox selected value (selectedvalue )?

Source: Internet
Author: User

I am used to ComboBox and ListBox in ASP. NET. I assume that ComboBox in SL, winform, and WPF should also have a property similar to selectedvalue, but the result is unfortunate!

However, this problem actually exists in winform. Here are three simple solutions:
1. Refer to examples)

2. Make good use of the tag attribute of comboboxitem. Since there is no value attribute, why not directly use the tag attribute as the value?
CodeAs follows:

Code
Cbo2.items. Add ( New Comboboxitem () {content =   " Content1 " , Tag =   " Tag1 " }); // Assignment
Txtmsg. Text =   " Value = "   + (Cbo2.selecteditem As Comboboxitem). Tag +   " ; Text = "   + (Cbo2.selecteditem As Comboboxitem). text; // Value

In this way, I personally feel a little slippery, but in most cases the problem can be solved.

3. Maybe this is the original intention of the MS designer. When I observed ComboBox. Items. Add (), I found that the add method accepts an object-type parameter. The object is the ancestor of everything! That is to say, Ms allows users

Play to your full potential, any extension, not only the value attribute, but also the age attribute and the birthday attribute...

Code: first define a basic class (you can change it to book, person, user, and other business classes as needed)

Code
Public   Class Itemobject {
Public   String Text { Set ; Get ;}
Public   String Value { Set ; Get ;}
}

Assignment:

Code
CBO. Items. Clear ();
For ( Int I =   0 ; I <   10 ; I ++ )
{
CBO. Items. Add ( New Itemobject () {text =   " Text "   + I. tostring (), value =   " Value "   + I. tostring ()});
}
CBO. displaymemberpath =   " Text " ;

 

Value: similar to the second method

Code
Txtmsg. Text =   " Value = "   + (CBO. selecteditem As Itemobject). Value +   " ; Text = "   + (CBO. selecteditem As Itemobject). text;

 

Welcome to reprint, reprint please indicate from the bodhi tree under the Yang over http://www.cnblogs.com/yjmyzz/archive/2009/02/12/1388865.html

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.