Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Icon to TreeViewItem

I have been asked to update a TreeVeiw in a WPF application that is built on the fly from a class object. As you will see treeveiw is not bound to anything. Below is not my code!!

<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);

    }

This builds the tree correctly and all I have been asked to do is add an icon to the TreeViewItem. The icon will be a different depending on whether it is a location or a printer within that location.

I cannot see how to add icons to TreeViewItems can anyone point me in the right direction?

like image 889
Fred Avatar asked Nov 29 '12 10:11

Fred


2 Answers

I solved this by changing this line

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

To

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

Adding this function

    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;
    }

Worked perfectly.

like image 152
Fred Avatar answered Oct 07 '22 17:10

Fred


Very nice. I just add this inside method wich return stack panel, to make code much readable:

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;
}

and in treeView Initialization

// Assign stack to header

item.Header = CustomizeTreeViewItem(itemObj);
like image 45
Ali Abdulhussein Avatar answered Oct 07 '22 18:10

Ali Abdulhussein