以编程方式将节点图像的背景设置为颜色

时间:2013-10-02 21:01:32

标签: c# wpf image background treeview

下面的代码生成一个树形视图节点,其中包含一个Icon(来自.PNG文件Piconfolder \ PiconName),后跟一个Text string 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;  
   }

1 个答案:

答案 0 :(得分:1)

替换

pan.Children.Add(image);

var iconPanel = new Grid(); // or other panel
iconPanel.Background = Brushes.Blue;
iconPanel.Children.Add(image);
pan.Children.Add(iconPanel);