else
tvNewGroupItem.HeaderText = contact.ContactGroup.Name;
itemObject = new ItemObject();
itemObject.contactGroup = contact.ContactGroup;
tvNewGroupItem.Tag = itemObject;
// 添加联系人组节点
tvContactList.Items.Add(tvNewGroupItem);
TreeViewIconsItem tvNewItem = new TreeViewIconsItem();
// 为联系人节点添加图标
tvNewItem.Icon = CreateImage(iconUri);
tvNewItem.HeaderText = contact.Name;
itemObject = new ItemObject();
itemObject.contact = contact;
tvNewItem.Tag = itemObject;
// 添加好联系人点
tvNewGroupItem.Items.Add(tvNewItem);
}
为了方便向树节点中添加图标,在addContactAndGroup方法中使用了一个TreeViewIconsItem类,该类是TreeViewItem类的子类,负责向树节点添加图标。该类的实现代码如下:
// 负责向树节点中添加图标
public class TreeViewIconsItem : TreeViewItem
{
ImageSource iconSource;
TextBlock textBlock;
Image icon;
public TreeViewIconsItem()
{
StackPanel stack = new StackPanel();
// 设置StackPanel中的内容水平排列
stack.Orientation = Orientation.Horizontal;
Header = stack;
icon = new Image();
icon.VerticalAlignment = VerticalAlignment.Center;
icon.Margin = new Thickness(0, 0, 4, 0);
icon.Source = iconSource;
// 向StackPanel对象中添加一个图标对象
stack.Children.Add(icon);
// 创建用于添加文本信息的TextBlock对象
textBlock = new TextBlock();
textBlock.VerticalAlignment = VerticalAlignment.Center;
// 向StackPanel对象中添加文本信息
stack.Children.Add(textBlock);
}
// 用于设置或获得节点中的图标对象
public ImageSource Icon
{
set
{
iconSource = value;
icon.Source = iconSource;
icon.Width = 16;
icon.Height = 16;
}
get
{
return iconSource;
} }
|