下面是ComboBox的简单实用
XAML:
<Grid>
<ComboBox Height=
"23"
Margin=
"12,12,0,0"
Name=
"comboBox1"
VerticalAlignment=
"Top"
HorizontalAlignment=
"Left"
Width=
"120"
/>
<Button Height=
"23"
Margin=
"21,53,69,0"
Name=
"button1"
VerticalAlignment=
"Top"
Click=
"button1_Click"
>Get ComboBox
select
value</Button>
</Grid>
|
C# Code:
public
partial
class
ComboBoxTest : Window
{
public
ComboBoxTest()
{
InitializeComponent();
Bind();
}
public
void
Bind()
{
IList<customer> customList =
new
List<customer>();
customList.Add(
new
customer() { ID = 3, Name =
"Tom"
});
customList.Add(
new
customer() { ID = 4, Name =
"Bob"
});
customList.Add(
new
customer() { ID = 5, Name =
"Cat"
});
comboBox1.ItemsSource = customList;
comboBox1.DisplayMemberPath =
"Name"
;
comboBox1.SelectedValuePath =
"ID"
;
comboBox1.SelectedValue = 4;
}
private
void
button1_Click(
object
sender, RoutedEventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
}
public
class
customer
{
public
int
ID{
get
;
set
;}
public
string
Name {
get
;
set
; }
}
|
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/11/23/2174462.html,如需转载请自行联系原作者