繁体   English   中英

将图标添加到TreeViewItem

[英]Adding Icon to TreeViewItem

我被要求更新一个在类对象中动态构建的WPF应用程序中的TreeVeiw。 正如您将看到treeveiw没有任何约束。 以下不是我的代码!!

<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添加图标,任何人都可以指向正确的方向吗?

我通过改变这一行来解决这个问题

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

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

工作得很完美。

非常好。 我只是在返回堆栈面板中添加这个内部方法,以使代码更具可读性:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM