(WPF学习记录)第十三章 ListBox选取
2009-07-03 14:27
344 查看
ListBox是经由Selector,继承自ItemsControl的3个控件之一:
Control
ItemsControl
Selector (abstract)
ComboBox
ListBox
TabControl
ListBox最重要的属性是Items。Items的类型是ItemCollection,这是object类型的collection,也就是说,任何对象都可以放在ListBox中。
第01个小程序:列举颜色名称(ListColorNames.cs)
运行结果如下:
第02个小程序:列举颜色值(ListColorValues.cs)
这种做法简化了事件处理函数,因为SelectedItem只需要被转成Color对象即可。ListBox会显示Color的ToString方法的返回值。运行结果如下:
第03个小程序:列举颜色名称
第1个代码文件:NamedColor.cs。
Control
ItemsControl
Selector (abstract)
ComboBox
ListBox
TabControl
ListBox最重要的属性是Items。Items的类型是ItemCollection,这是object类型的collection,也就是说,任何对象都可以放在ListBox中。
第01个小程序:列举颜色名称(ListColorNames.cs)
using System; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace Chapter13 { class ListColorNames : Window { [STAThread] public static void Main() { Application app = new Application(); app.Run(new ListColorNames()); } public ListColorNames() { Title = "List Color Names"; Width = 480; Height = 288; WindowStartupLocation = WindowStartupLocation.CenterScreen; // 创建一个ListBox ListBox lstbox = new ListBox(); lstbox.Width = 150; lstbox.Height = 150; lstbox.SelectionChanged += ListBoxOnSelectionChanged; Content = lstbox; // 将颜色填入ListBox PropertyInfo[] props = typeof(Colors).GetProperties(); foreach (PropertyInfo prop in props) lstbox.Items.Add(prop.Name); // 让程序一开始就具有输入焦点,并显示所选取的项目 lstbox.SelectedItem = "Magenta"; lstbox.ScrollIntoView(lstbox.SelectedItem); //滚动ListBox,让被选取项目可见 lstbox.Focus(); } void ListBoxOnSelectionChanged(object sender, SelectionChangedEventArgs args) { ListBox lstbox = sender as ListBox; string str = lstbox.SelectedItem as string; if (str != null) { Color clr = (Color)typeof(Colors).GetProperty(str).GetValue(null, null); Background = new SolidColorBrush(clr); } } } }
运行结果如下:
第02个小程序:列举颜色值(ListColorValues.cs)
using System; using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Controls; using System.Windows.Media; namespace Chapter13 { class ListColorValues : Window { [STAThread] public static void Main() { Application app = new Application(); app.Run(new ListColorValues()); } public ListColorValues() { Title = "List Color Values"; Width = 480; Height = 288; WindowStartupLocation = WindowStartupLocation.CenterScreen; // 创建ListBox ListBox lstbox = new ListBox(); lstbox.Width = 150; lstbox.Height = 150; lstbox.SelectionChanged += ListBoxOnSelectionChanged; Content = lstbox; // 将颜色值填入ListBox PropertyInfo[] props = typeof(Colors).GetProperties(); foreach (PropertyInfo prop in props) lstbox.Items.Add(prop.GetValue(null, null)); } void ListBoxOnSelectionChanged(object sender, SelectionChangedEventArgs args) { ListBox lstbox = sender as ListBox; if (lstbox.SelectedIndex != -1) { Color clr = (Color)lstbox.SelectedItem; Background = new SolidColorBrush(clr); } } } }
这种做法简化了事件处理函数,因为SelectedItem只需要被转成Color对象即可。ListBox会显示Color的ToString方法的返回值。运行结果如下:
第03个小程序:列举颜色名称
第1个代码文件:NamedColor.cs。
using System; using System.Reflection; using System.Windows.Media; namespace Chapter13 { class NamedColor { static NamedColor[] nclrs; Color clr; string str; // 静态构造函数 static NamedColor() { PropertyInfo[] props = typeof(Colors).GetProperties(); nclrs = new NamedColor[props.Length]; for (int i = 0; i < props.Length; i++) nclrs[i] = new NamedColor(props[i].Name, (Color)props[i].GetValue(null, null)); } // 私有带参构造函数 private NamedColor(string str, Color clr) { this.str = str; this.clr = clr; } // 静态只读属性 public static NamedColor[] All { get { return nclrs; } } public Color Color { get { return clr; } } public string Name { get { string strSpaced = str[0].ToString(); // 在颜色名称中插入空格,使得颜色名称由真正的单词组成。如"AliceBlue"变为"Alice Blue"。 for (int i = 1; i < str.Length; i++) //从第2个字符开始判断 strSpaced += (char.IsUpper(str[i]) ? " " : "") + str[i].ToString(); return strSpaced; } } // Override of ToString method. public override string ToString() { return str; } } }
相关文章推荐
- wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》
- wpf的学习记录1
- WPF入门教程系列一——基础 一、 前言 最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料。为了温故而知新把学习过程记录下来,以备后
- WPF 学习记录——源码实例
- WPF学习笔记——ListBox用ItemsSource绑定数据源
- WPF学习总结和记录(七)-尺寸缩放 定位 上
- (WPF学习记录)第七章 Canvas
- WPF 学习记录——Application
- (WPF学习记录)第十章 自定义Element
- WPF 系列学习之-------ListBox
- wpf学习笔记(1)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(2)《都是自己根据网络资源学习记录的仅供参考》
- wpf的binding学习记录
- WPF学习总结和记录(八)-尺寸缩放 定位 下
- WPF学习记录1
- WPF 学习记录——Buttons and Other Controls
- WPF学习总结和记录(四)Items控件下
- wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
- 黑马程序员--wpf学习记录笔记一
- WPF学习笔记——设置ListBox选中项的背景颜色