C#学习之四---ListBox

来源:互联网 发布:长沙智工软件 编辑:程序博客网 时间:2024/05/18 13:48

呵呵  没想到在C#中做ListBox这么简单,不多说了,马上写出我今天实践做的ListBox.

建设WPF工程我就不重复了,直接将ListBox,
在工具箱中调出ListBox控件,选中它,F4,调出其对应的属性,将其名称改为myList;

在属性中找到下图 Ttems   ,点击后面的3点框。

会弹出一个对话框,我们关心的是我们对话框的左下方,如下图:

选择ListBoxItem;

添加6个ListBoxItem,如图:

在每个ListBoxItem的属性中将Content改为对应的myList1---myList6,还可以根据自己的爱好改变对应的颜色;

最后我们的应用程序做出来了,如图:

额  ListBox没有左侧的下来条啊,不好看.

呵呵  为了可以见到下来条,我们把ListBox缩小,如图:

这样我们就可以看到下拉条了,

最后的成果图:

为了可以实现ListBox的选择作用:

我们加入代码,关键部分的如下:

ListBoxItem selectedList = (myList.SelectedItem as ListBoxItem );
这代码就是读取对应的选择List.

这段代码和上一段是等效的:

 ListBoxItem selectedlist = (ListBoxItem)myList.SelectedItem;

最后整体代码如下,用的是switch语句;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace myList{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)        {            ListBoxItem selectedList = (myList.SelectedItem as ListBoxItem );            switch (selectedList.Content.ToString())            {                 case "myList1":                    showMyList1();                    break;                case "myList2":                    showMyList2();                    break;                case "myList3":                    showMyList3();                    break;                case "myList4":                    showMyList4();                    break;                case "myList5":                    showMyList5();                    break;                case "myList6":                    showMyList6();                    break;                default :                    showMyList();                    break;            }                }                private void showMyList1()        {            textName.Text ="myList1";        }        private void showMyList2()        {            textName.Text = "myList2";        }        private void showMyList3()        {            textName.Text = "myList3";        }        private void showMyList4()        {            textName.Text = "myList4";        }        private void showMyList5()        {            textName.Text = "myList5";        }        private void showMyList6()        {            textName.Text = "myList6";        }        private void showMyList()        {            textName.Text = "Wrong List!";        }    }}

最后是应用程序的效果图:



我们下拉下拉条,选择myList6,空白框显示了我们选择的myList6,

这就是我们今天的ListBox实践,


OVER!!!



原创粉丝点击