您的位置:首页 > 其它

WPF 控件使用之ComboBox

2011-11-23 10:38 337 查看
下面是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; }
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: