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