下面的代码生成一个带有图标(来自 .PNG 文件 Piconfolder\PiconName)后跟文本字符串 Pheader 的树视图节点。我想为图标添加背景(与透明图标一起使用)
public static TreeViewItem CreateTreeViewItem(
string Pheader,
string PiconFolder,
string PiconName)
{
string iconFolder = PiconFolder;
string iconName = PiconName;
string header = Pheader;
TreeViewItem child = new TreeViewItem();
StackPanel pan = new StackPanel();
pan.Orientation = Orientation.Horizontal;
string fPath = System.IO.Path.Combine(iconFolder, iconName);
Image image = new Image();
image.Height = 16;
image.Width = 16;
image.Source = new BitmapImage(new Uri(fPath, uriKind.RelativeOrAbsolute));
pan.Children.Add(image);
pan.Children.Add(new TextBlock(new Run(" " + header)));
child.Header = pan;
}