WPF提供了许多包装集合的控件。这里包括了ListBox列表控件、ComboBox组合框控件,还有其他的更多的空间我们就不介绍了。
1、ListBox
ListBox就是包含了列表的一个控件,我们用WPF程序来说明如何使用。
ListBox主要通过ListItem这个属性来创建元素,可以包括文本,图片和按钮等等其他控件,也可以包括一些布局面板。
我们可以通过ListBox的SelectedIndex和SelectedItem属性访问选中对象的下标和选中的对象,我们创建三个复选框为例说明。
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listBox1.SelectedItem == null) return;
this.textSelection.Text = "选择的选项为:" + (listBox1.SelectedIndex+1) + "\r\n"
+ "选中状态为:" + ((CheckBox)listBox1.SelectedItem).IsChecked;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (CheckBox item in listBox1.Items)
{
if (item.IsChecked == true)