c# - 将图标添加到 TreeViewItem

标签 c# treeview wpf-controls

我被要求更新 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;
    }

完美运行。

关于c# - 将图标添加到 TreeViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13623178/

相关文章:

c# - Entity Framework Fluent API : An object of type 'IndexAttribute' cannot be serialized by the IndexAnnotationSerializer.

c# - 使用 LINQ 过滤 DirectoryInfo 的 ObservableCollection

c# - 如何从 RGB 颜色创建 WPF 图像?

WPF UserControl 或 ControlTemplate...(不确定)

WPF 绑定(bind)渲染 Gui 进度

wpf - 对 DevExpress WPF 控件的指控是否有效?什么是好的替代供应商?

c# - 如何使用 Microsoft Graph API 获取用户所属的组名?

c# - 什么类似于 c# 中的向左旋转?

c# - 强制构建因某些条件而失败

treeview - Vala:我怎么知道编辑过的信号是从哪一列发出的?