您的位置:首页 > 其它

(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)
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: