.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Treeview - how to change node icon depending on node contents

Posted By:      Posted Date: September 01, 2010    Points: 0   Category :WPF
I have a treeview in an MVVM application: <TreeView ItemsSource="{Binding RootNodes}"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> <Setter Property="FontWeight" Value="Normal" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontWeight" Value="Bold" /> </Trigger> </Style.Triggers> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <TextBlock Text="{Binding NodeName}" /> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> The treeview is bound to an ObservableCollection called RootNodes. Elements in the RootNodes collection expose a member "NodeName" which is the text for the nodes in the treeview at runtime. They also expose a member called "NodeType", effectively an enumeration that indicates what type of icon should be displa

View Complete Post

More Related Resource Links

treeview selected node font style does not change


Hi All,

I have a treeview and I am giving the node style in code behind.

Treeview design is as below:

<asp:TreeView ID="tvIARHierarchies" runat="server" NodeIndent="15" ShowExpandCollapse="true"
                                        ShowLines="True" Width="98%" OnSelectedNodeChanged="tvIARHierarchies_SelectedNodeChanged">
                                        <ParentNodeStyle Font-Bold="False" />
                                        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />

(Imperfect) Solution to TreeView Postback / Client-Side Node Selection Issue


I've seen this question posted on all corners of the web... "How do I disable "AutoPostBack" on the ASP.NET 2.0 TreeView control?!"  Unfortunately, most of the answers either don't work, miss the point (don't work right), or boil down to "You can't do it."  I happen to know that this behavior worked perfectly in the IEWebControls TreeView that Microsoft released as an add-on prior to ASP.NET 2.0, however.  So I set out to find a solution for the ASP.NET 2.0 TreeView issue.

In a nutshell, the behavior that I and others are looking for is to have a TreeView rendered in a page that allows the user to expand, contract, and select nodes within the TreeView without causing a PostBack to the server.  This implies two things: (1) the most recently-selected node should be recorded as the currently-selected node [i.e., at PostBack time, the most-recently selected node is actually represented as the SelectedNode]; and (2) the selected node should exhibit the SelectedNodeStyle as specified in the TreeView declaration.

It appears that the TreeView engineers saw only two possible outcomes from selecting a leaf node in a tree: (1) immediately posting back to the server, or (2) immediately navigating to another web page.  They apparently did not consider option (3) just allowing selection in-place on the client, while waiting for a click on so

How can i update treeview root node every time based on the record entries

Hi all, I will have an mdi form and with a treeview which has it's initial property as visible =false. If i select an option from menu a child form will exists i will fill some details and click on save when ever click on save i will make the root node of the treeview to some name as File[batch count:0]. Now if i select another option from menu another form will appear and if i fill it with some details and click on save i would like to change the root node name to File[batch count:1] and a child node should exists with some name

On escape key while editing a treeview node

On hitting escape key while editing a treeview i would like to cancel the editing a would like to show the default name that was previously there. Where should i handle this can any one tell me please..    

Last node in TreeView

Hi everybody.... How are you.... i think everything is all right...... now i have a question.... how to get the last node in treeview ??????? Thankssss.....

Last node in TreeView

Hi everybody.... How are you.... i think everything is all right...... now i have a question.... how to get the last node in treeview ??????? Thankssss.....

drag and drop treeview node to textarea in web forms

Hi We have an asp.net treeview control and a texarea. The childnodes of treeview need to be draggable and can be dropped into txtarea.   Please let us know how it will be achieved through JQuery?

Treeview node to add onclick event to download files

 Hi, I would like to know how to add onclick evend to a TreeNode so that the file can be downloaded.   How can I implement below sample code to a TreeNode? <a href="#" onclick="popup('/directory1/Somefile.doc'); return false;" target="download"> Somefile </a> <script language="javascript"> function popup(url) { popWin = window.parent.open(url, 'popWindow', 'height=900, width=900, toolbar=no, menubar=yes, scrollbars=yes, titlebar=no, resizable=yes,location=0, directories=no, status=no'); } </script> I have TreeNodePopulate event to populate nodes like this: protected void DocRepository_TreeNodePopulate(object sender, TreeNodeEventArgs e) { .... String[] files = Directory.GetFiles(fullPath); foreach(String file in files) { TreeNode newNode  = new TreeNode(Path.GetFileName(file), Path.GetFileName(file)); node.ChildNodes.Add(newNode); } }   Thank you in advance.              

Display error if a treeview root node contains a child node

I am having a treeview initially with a root node when form is loaded. I will add child node as some.txt file at the runtime by selecting an option as Addnew from contextmenu which was displayed when the user right clicks on the root node. Now what i need is if the tree has child node appended to the Root and if user tries to create a new node by clicking the option addnew from context menu i would like to display an error as only one child allowed.   Can any one help

TreeView node visually unselected after selecting tree node and then clicking soem other field



I am using .NET 3.5 (VS 2008).  I have a SplitContainer with a TreeView on one side and a form with various fields, tabs, etc on the other.  When I select one of the tree nodes, the node is highlighted.  The node loses its highlighting when the user clicks on one of the tabs or enters a field on the other side of the SplitContainer.  Although no highlighting is visible, the treeView.SelectNode() still returns the proper node, so it appears that the tree node is still selected, just not visually highlighted.  The problem is that without the highlighting to distinguish which node is selected, the user can easily get lost.  Is there a build-in mechanism to maintain some sort of highlighting on the selected node when the cursor is moved to another form.  For example, in Windows Explorer, if you select a tree node, the node is highlighted in a dark blue.  If you were to then place the cursor in the Address box, the selected node changes to have a beige color.  This still shows which node was selected.

Is there a built-in solution (ie some property to set on the tree view or nodes) that would help fix my issue or do I have to build my own solution to mimic something like what Windows Explorer does?  I wanted to avoid having to put event handlers on all tabs and form fields that would be used to &

Replace node contents using xmldocument




Attempting to replace the contents of a node, but not having much luck..

I need to look at my Keywords node, if it has the word 'test,' in it then replace it with ""

Essentially I'm trying to delete one of the keywords - they will always have a comma after each word as well.

So I'm trying to say in code: For any elements that is in the department Claims, get the Keywords & if it contains the string 'test,' replace it with nothing.


My elements look like this:

    <Link Type="Page" Target=""><![CDATA[]]></Link>

..additional elements just like this one..


My code like this:


TreeView, ContextMenu, how do I get the SelectedItem's data Node/object?


Hi all,

I've implemented a tree and have bound it to my treeview. the binding is fine, but id like to add "add child" "remove node" functionality in a context menu for a selected item. I can not for the life of me figure out how to do this. My code is below. please help me. please point me in the right direction.


<TreeView Name="treeView" Margin="10,10,14,10" BorderThickness="2" ItemsSource="{Binding Nodes}">

How to handle treeview selected node changed event



From my below sample code, I don't want to use Sessions to handle each click event inside treeview selected node changed.

Secondly, if you suggest me to use treenode.SelectedValue then I would say my values are dyanmically generated.

Everytime a node a clicked, the selected node value is sent in the next request xml to get response and xml schema is different for each click. How do I handle this each click event other than using session variables. I thought of using Request.Item (and integrating with URL redirect property) but I don't want to use that.


Dim trNode As New TreeNode
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then        
            trNode.Text = "Not Postback"
            Session("Flag") = 1
        End If
    End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
        If Session.Item("Flag") = 1 Then
            trNode.Text = "Is PostBack"
            Session("Flag") = 2
        End If

Setting CssClass on every node in TreeView element



I would like to set a class for every node that exists on my treeview, so I can change its appearance through CSS only. The TreeNode element is rendered as a table with <td> and <tr> tags. Basically I would like to add "class" attribute to those tags. 

So far I wasn't able to do that, because I couldn't find a suitable property on the treenode element. There IS a CssClass property on the whole treeview but that doesn't help me at all. Also I noticed the NodeStyle and SelectedNodeStyle properties on the TreeView element, but that means that I would have to change the appearance programatically, not with CSS.

Anyone got any ideas?

Displaying + on treeview node even when no child nodes are present


I have a treeview control that is working great, except that I would like the behavior of some nodes to display the + even though no child nodes exist yet. Meaning, I want my treeview control to look like this:

- RootNode
    + Node1
    + Node2
    + Node3

However, Nodes 1, 2, and 3 do not have any child nodes. From what I can see the + or - does not display if there are no child nodes, but I would like to see the + appear even though there are no child nodes. I want to do this because I want to create the child nodes upon expanding the parent node (Node1 for example).

Is this possible?



Cluster Node Change


Hello Friends/Colleagues,


I have 2 nodes active/active with 3 virtual instances.

2 instances on node 1 and 3d instance on node 2.


Sometimes they automatically change from node 1 to node 2.

Is there any way that I get some notification regarding the instance that have changed from one node to another?


I found following may be probable solution:

EXEC master..xp_cmdshell 'cluster resource "SQL Server (Instance Name)"'

Result: Instance Name, Node Name

Put results into table..........Again run the command.....compare.......too much stuff.....

If someone knows the easy path to this.......


How could I get the information that has been changed?


Your help would be really great!



treeview node selection takes a lot of time


Hi All,

I have a tree-view which doesn't have any events and AutoPostBack property is also false. But whenever the treeview node is selected,its taking a lot of time...The tree view is placed inside update panel...I have an update progress and selection of any node shows the progress for atleast 5-6 seconds.Please suggest what can be done....

The code is as below :

<asp:UpdatePanel ID="updatePnlListBoxes" runat="server">
<table align="center">
<asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="updatePnlListBoxes">
 <img id="Img2" src="~/images/ajax-loader.gif" alt="" runat="server" />
 <asp:TreeView ID="tvNodes" runat="server" NodeIndent="15" ShowExpandCollapse="true"
ShowLines="True" Width="181px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True&

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend