0

下面的代码生成一个带有图标(来自 .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;  
   }
4

1 回答 1

1

代替

pan.Children.Add(image);

var iconPanel = new Grid(); // or other panel
iconPanel.Background = Brushes.Blue;
iconPanel.Children.Add(image);
pan.Children.Add(iconPanel);    
于 2013-10-02T21:11:54.690 回答