在TreeViewItem中添加图标

5
我被要求更新一个WPF应用程序中的TreeView,该TreeView是从类对象动态构建的。正如您所看到的,TreeView并没有绑定到任何东西。以下不是我的代码! <TreeView Grid.Row="0" HorizontalAlignment="Stretch" Name="tvLocations" VerticalAlignment="Stretch" SelectedItemChanged="tvLocations_SelectedItemChanged" />
    private void BuildTreeVeiw(Location locationList)
    {
        this.Title = _selectedLoc.Name +  " - Locations";
        tvLocations.Items.Clear();

        TreeViewItem tvitem;

        tvitem = new TreeViewItem() { Header = locationList.Name, Uid = locationList.Id.ToString() };

        if (locationList.Printers != null)
        {
            TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
            tvprnitem.FontWeight = FontWeights.Regular;

            foreach (Printer sprinters in locationList.Printers)
            {
                TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
                TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
                psubitem.Items.Add(psubitem1);
                tvprnitem.Items.Add(psubitem);
            }
            tvitem.Items.Add(tvprnitem);
        }

        foreach (Location loc in locationList.Children)
        {
            AddChildren(loc, ref tvitem);
        }
        tvLocations.Items.Add(tvitem);
    }

    private void AddChildren(Location child, ref TreeViewItem tvi)
    {
        TreeViewItem tvitem;

        tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };
        if (child.Name ==  _currentLocation.Name)
        {
            tvitem.FontWeight = FontWeights.Bold;
        }

        if (child.Printers != null)
        {
            TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
            tvprnitem.FontWeight = FontWeights.Regular;

            foreach (Printer sprinters in child.Printers)
            {
                TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
                TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
                psubitem.Items.Add(psubitem1);
                tvprnitem.Items.Add(psubitem);
            }
            tvitem.Items.Add(tvprnitem);
        }
        if (child.Children != null)
        {
            foreach (Location loc in child.Children)
            {
                AddChildren(loc, ref tvitem);
            }
        }

        tvi.Items.Add(tvitem);

    }

这样可以正确地构建树,而我被要求做的就是在TreeViewItem中添加一个图标。该图标将根据它是位置还是该位置内的打印机而不同。

我不知道如何向TreeViewItems添加图标,请问有什么方法吗?


为什么不使用数据绑定?使用绑定可以更轻松地处理 WPF 控件。 - Dennis
是的,但我希望添加一个图标会是一个小改变。整个应用程序需要重写,但时间不允许。 - Fred
2个回答

16
我通过更改此行解决了这个问题。
tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };

To

tvitem = GetTreeView(child.Id.ToString(), child.Name, "location.png");

添加此功能

    private TreeViewItem GetTreeView(string uid, string text, string imagePath)
    {
        TreeViewItem item = new TreeViewItem();
        item.Uid = uid;
        item.IsExpanded = false;

        // create stack panel
        StackPanel stack = new StackPanel();
        stack.Orientation = Orientation.Horizontal;

        // create Image
        Image image = new Image();
        image.Source = new BitmapImage
            (new Uri("pack://application:,,/Images/" + imagePath));
        image.Width = 16;
        image.Height = 16;
        // Label
        Label lbl = new Label();
        lbl.Content = text;


        // Add into stack
        stack.Children.Add(image);
        stack.Children.Add(lbl);

        // assign stack to header
        item.Header = stack;
        return item;
    }

完美地运行。


使用TextBlock可能会更容易,请参见此答案中的评论线程。很奇怪,但我注意到TextBlock尊重根节点的主题选择颜色,而Label则不尊重。 - jrh

0
非常好。 我只是在返回堆栈面板的方法内添加了这个,以使代码更易读:
private StackPanel CustomizeTreeViewItem(object itemObj)
{
     // Add Icon
     // Create Stack Panel
     StackPanel stkPanel = new StackPanel();
     stkPanel.Orientation = Orientation.Horizontal;

     // Create Image
     Image img = new Image();
     img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/control.png"));
     img.Width = 16;
     img.Height = 16;

     // Create TextBlock
     TextBlock lbl = new TextBlock();
     lbl.Text = itemObj.ToString();

     // Add to stack
     stkPanel.Children.Add(img);
     stkPanel.Children.Add(lbl);

     return stkPanel;
}

在treeView初始化中。
// Assign stack to header

item.Header = CustomizeTreeViewItem(itemObj);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接