下面是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; } }