TreeView LightVisualElement

1 Answer 23 Views
Treeview
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Mark asked on 31 Mar 2025, 07:56 PM

We have been using the TreeView in our software for some time now. However, recently, we noticed that it's not behaving as we have coded it. We had code that changed the Forcolor and Font when a node was selected. However, this doesn't seem to be working anymore. Attached is a sample code. Any help would be greatly appreciated.

 

FYI, we are using Telerik for Winforms: 2024.3.924.462

TIA



using System;
using System.Collections.Generic;
using System.Drawing;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace TestNodeColorChange
{
   public partial class RadForm1 : Telerik.WinControls.UI.RadForm
   {
      #region Private Fields

      private string selectedNodeKey = string.Empty;

      #endregion Private Fields

      #region Public Constructors

      public RadForm1()
      {
         InitializeComponent();
         Load += FormLoading;
      }

      #endregion Public Constructors

      #region Private Methods

      private void FormLoading(object sender, EventArgs e)
      {
         List<TreeViewData> list = new List<TreeViewData>()
         {
            new TreeViewData { key = 1, DisplayValue = "Level 1" },
            new TreeViewData { key = 2, parentKey = 1, DisplayValue = "Branch A" },
            new TreeViewData { key = 3, parentKey = 1, DisplayValue = "Branch B" },
            new TreeViewData { key = 4, parentKey = 1, DisplayValue = "Branch C" }
         };

         radTreeView1.ChildMember = "key";
         radTreeView1.DisplayMember = "DisplayValue";
         radTreeView1.ParentMember = "parentKey";
         radTreeView1.DataSource = list;

         radTreeView1.ExpandAll();

         radTreeView1.SelectedNodeChanged += SelectedNodeChanged;
         radTreeView1.NodeFormatting += NodeFormatting;
      }

      private void NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
      {
         if (e.NodeElement is LightVisualElement lve)
         {
            lve.ResetValue(VisualElement.ForeColorProperty, ValueResetFlags.Local);
            lve.ResetValue(VisualElement.FontProperty, ValueResetFlags.Local);

            if (e.Node.Value.ToString() == selectedNodeKey)
            {
               lve.Font = new Font(lve.Font.FontFamily, lve.Font.Size, FontStyle.Bold);
               lve.ForeColor = Color.Purple;
               radTreeView1.NodeFormatting -= NodeFormatting;
               radTreeView1.Refresh();
               radTreeView1.NodeFormatting += NodeFormatting;
               Console.WriteLine("NodeFormatting: " + selectedNodeKey);
               Console.WriteLine("NodeFormatting: " + e.NodeElement.ForeColor.ToString());
               Console.WriteLine("NodeFormatting: " + ((e.NodeElement.Font.Style & FontStyle.Bold) != 0));
            }
         }
      }

      private void SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
      {
         selectedNodeKey = e.Node.Value.ToString();
         Console.WriteLine("SelectedNodeChanged: " + selectedNodeKey);
         radTreeView1.Nodes.Refresh();
      }

      #endregion Private Methods

      #region Internal Classes

      internal class TreeViewData
      {
         #region Public Properties

         public string DisplayValue { get; set; }
         public int key { get; set; }
         public int? parentKey { get; set; }

         #endregion Public Properties
      }

      #endregion Internal Classes
   }
}

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 03 Apr 2025, 12:13 PM

Hello, Mark,

Thank you for providing a code snippet of what you have currently used. 

I reviewed the provided code snippet and it seems to me that you are have complicated the code in the NodeFormatting event. If your intention is to change the font and forecolor when a node gets selected, you can use the following code snippet. I believe it is more simple and should be suitable for your case. It is not necessary to call the Refresh method in SelectedNodeChanged as well.

private void NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
{
    if (e.Node.Selected)
    {
        e.NodeElement.Font = new Font(e.NodeElement.Font.FontFamily, e.NodeElement.Font.Size, FontStyle.Bold);
        e.NodeElement.ContentElement.ForeColor = Color.Purple;
    }
    else
    {
        e.NodeElement.ResetValue(LightVisualElement.FontProperty, ValueResetFlags.Local);
        e.NodeElement.ContentElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
    }
}

Can you give this solution a try and let me know how it works for you?

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
commented on 03 Apr 2025, 09:25 PM

You know what, I figured it out 10 minutes after I submitted my comment.  I kept searching your help files and found that we now need to access the ContentElment.   This was an easy fix for us.

 

Thanks

Tags
Treeview
Asked by
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or