代码如下:
TreeViewItem CreateTreeViewItem(MyClass cls)
{
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"pack://application:,,/Angel.png");
bitmap.EndInit();
Image img = new Image();
img.Width = img.Height = 20;
img.Source = bitmap;
TextBlock tb = new TextBlock();
tb.Text = cls.Name;
panel.Children.Add(img);
panel.Children.Add(tb);
TreeViewItem tvi = new TreeViewItem();
tvi.Header = panel;
tvi.Tag = cls;
return tvi;
}