以编程方式设置多色 treeviewitem 标题 [英] Set multi colour treeviewitem header programmatically

查看:23
本文介绍了以编程方式设置多色 treeviewitem 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过代码构建了一个 TreeView 以匹配一个 xml 文档,并且我正在通过代码设置每个 TreeViewItem 的标头以匹配标签名称和(如果适用)一个重要属性.

I build a TreeView through code to match an xml document and I'm setting the header of each TreeViewItem through code to match the tag name and (if applicable) a significant attribute.

虽然看起来有点无聊,所以我想将标签名称设置为一种颜色,将属性值设置为不同的颜色,但我不知道这是否可行.

It looks a bit boring though so I would like to set the tag name to be one colour and the attribute value to be a different colour, but I can't figure out if that is even possible.

任何人都可以启发我,这可能吗?如果可能,我将如何处理?

Can anyone enlighten me, is this possible and if so how would I go about it?

编辑

为了澄清我想要做的是:

Just to clarify what I'd like to do is:

[blue]name - [/blue][yellow]属性[/yellow]

[blue]name - [/blue][yellow]attribute[/yellow]

所以第一个资产,资产 - "是蓝色的,手"是黄色的.

So the first asset, would have "Asset - " be blue, and "hands" be yellow.

EDIT2

根据大众需求,这里是我用来创建 TreeView 的代码 :)

By popular demand here is the code I'm using to create the TreeView :)

private TreeView CreateTree() {
    xmlDocument = XDocument.Load(FullPath, LoadOptions.SetLineInfo);
    xmlDocument.DescendantNodes().OfType < XComment > ().Remove();

    var tree = new TreeView {
        Name = "treeview_1",
        Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#1e1e1e")
    };


    var firstNode = xmlDocument.Descendants().First();
    var treeItm = new TreeViewItem {
        Header = Utilities.GenerateName(firstNode),
        Tag = firstNode.AbsoluteXPath(),
        Foreground = Brushes.WhiteSmoke
    };

    treeItm.Selected += NodeSelected;
    tree.Items.Add(treeItm);
    AddNodes(xmlDocument.Descendants().First(), treeItm);
    return tree;
}

private void AddNodes(XElement lastNode, TreeViewItem lastTreeItm) {
    var xElements = lastNode.Descendants().ToList();
    if (xElements.Any()) {
        var treeItm = new TreeViewItem {
            Header = Utilities.GenerateName(xElements.First()),
            Tag = xElements.First().AbsoluteXPath(),
            Foreground = Brushes.WhiteSmoke
        };
        treeItm.Selected += NodeSelected;
        lastTreeItm.Items.Add(treeItm);
        AddNodes(xElements.First(), treeItm);
    }


    var sibl = (XElement) lastNode.NextNode;
    if (sibl != null) {
        var treeItm = new TreeViewItem {
            Header = Utilities.GenerateName(sibl),
            Tag = sibl.AbsoluteXPath(),
            Foreground = Brushes.WhiteSmoke
        };
        treeItm.Selected += NodeSelected;
        ((TreeViewItem) lastTreeItm.Parent).Items.Add(treeItm);
        AddNodes(sibl, treeItm);
    }
}

EDIT3

这是名称生成的代码,我忘了在最新的编辑中添加它.我已经从 switch case 中删除了大部分代码,因为它只是更多相同.

Here's the code for name generation, I forgot to add that in the latest edit. I've removed most of the code from the switch case as it's just more of the same.

public class Utilities
{
    public static string GenerateName(XElement node)
    {
        switch (node.Name.LocalName)
        {
            case "Time":
                return "Time - " + GetAttr(node, "Id");
            default:
                if(node.Attribute("Id")?.Value != null)
                    return node.Name.LocalName + " - " + node.Attribute("Id")?.Value;
                return node.Name.LocalName;
        }   
    }

    private static string GetAttr(XElement node, string id)
    {
        return node.Attribute(id) != null ? node.Attribute(id)?.Value : "";
    }
}

推荐答案

            <TreeViewItem>
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Asset - " Foreground="Blue"/>
                        <TextBlock Text="hands" Foreground="Yellow"/>
                    </StackPanel>
                </TreeViewItem.Header>
            </TreeViewItem>

这应该是你要找的.通过使用 StackPanel,您可以设置两个具有不同前景色的 TextBlock.

This should be what you are looking for. By using the StackPanel, you can set up two TextBlocks with different Forecolors.

编辑

由于您是在 C# 中执行此操作,因此您主要只需要更改获取每个 TreeViewItem 的 Header 属性的方式.

Since you are doing this in C#, you mainly just need to change the way that you are getting the Header property for each of your TreeViewItems.

看起来您使用 Utilities.GenerateName() 来获取标题.您可以将名称引入并将它们添加到字符串中,而不是直接执行此操作

It looks like you use Utilities.GenerateName() to get the Headers. Instead of doing this directly, you could bring the names in and add them to a string

(String fullHeader = Utilities.GenerateName()).

然后使用一些逻辑来确定字符串需要如何拆分.假设您有 2 个值(即 String.Split() 返回一个大小为 2 的字符串数组),然后,您只需使用所需的 Foreground 属性创建 TextBlocks 并将文本属性设置为新的字符串.

Then use some logic to determine how the string needs to be split. Assuming you had 2 values ( I.E. String.Split() returned an array of Strings with a size of 2), you would then just make TextBlocks with the desired Foreground property and set the text properties to the new Strings.

TextBlock1.Text = YourStringArray[0]TextBlock2.Text = YourStringArray[1].

逻辑可以是您想要拆分字符串的任何方式,并且根据您获取名称的方式,您甚至可以在 Utilities.GenerateName() 方法中设置逻辑.

The logic can be however you want to split the strings and based on how you are getting the names, you could even set up the logic in your Utilities.GenerateName() method.

要实现您想要的功能,您需要的最大部分是 StackPanel.

The biggest part you will need to accomplish what you want is the StackPanel.

例如,您可以执行以下操作:

As an example, you could do something like this:

StackPanel myStackPanel = new StackPanel();
myStackPanel.Orientation = Orientation.Horizontal;
var treeItm = new TreeViewItem
{
    Header = myStackPanel,
    Tag = xElements.First().AbsoluteXPath(),
    Foreground = Brushes.WhiteSmoke
}

这篇关于以编程方式设置多色 treeviewitem 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆