代码改变世界

如果利用代码添加带图标的treeviewItem?

2007-02-05 09:43  爱车龟速兔  阅读(666)  评论(0编辑  收藏  举报

代码如下:

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;

        }