Prevent automatic selection of the primary TreeView node - c#

I have multiple tabs which represent filters for a specific view. One of those tabs is a TreeView called SOTree.
There's a button to clear all filters.
If I click a node in SOTree, it highlights said node and filters the view. I can then clear all filters, which also clears the selected nodes:
this.SelectedNodes.Clear();
this.SelectedNode = null;
At first, it seems to clear the selected nodes just fine. However, when I change the focus to a different window/tab and back, the first node of the treeview will be highlighted.
This causes two issues:
The user might think that the filter is still active, filtering for the first node
Clicking the node doesn't actually activate the filter, since the selection does not change. The user will have to select a different node first.
How can I prevent this automatic and unwanted highlighting of the node?

Related

Hide/disable some node in the treeview according to some conditions

I have three levels (one , two, three) represented as a radiobuttonlist. Also, I have treeview represents these levels (each level represented as a parent node and each level has a child)
My goal is: If the user check on level one in the radibutonlist, the treeview will hide or disable level two and three. if the user check on level two in the radio button list, the treeview will hide or disable level three.
I did not find code to hide or disable nodes in treeview based on conditions. could you please help me?
You have to be more specific. Where do you want to do that? On server side with C#, in client side with your own javascript or in client side using jquery?
Doing that on server side with c# you can use the RadioButtonList.SelectedIndexChanged event to rebuild the tree in any way you want, supposing the tree you refer is TreeView from ASP.NET.
You can add/remove TreeNodes to the TreeView or you can set, for each TreeNode, the SelectAction event you want (Expand, None, Select, SelectExpand).

Add a treeview item and restore tree status

This is my usecase: I've a treeview and I'm navigating them. At a certain moment I add programmatically an item.
At the moment after the item is added the tree completely collapse.
I'd like rather to have the same situation that was before the adding (the previous selected item still selected).
Is it possible?
Thanks!

How to add tri state checkboxes to treeview in asp.net

I need to have a treeview control in asp.net with tri state checkboxes using c#. Any pointers would be helpful.
In cases where you want users to be able to select multiple nodes, you can use the TreeView control to display a check box next to a node image. When the ShowCheckBoxes property is set to a TreeNodeType other than TreeNodeType.None, check boxes are displayed next to the specified nodes. When check boxes are displayed, you can use the TreeNodeCheckChanged event to run your custom routine whenever the state of a check box changes between posts to the server. The next time the page is posted to the server, the CheckedNodes property is populated with the selected nodes.
You can use either for the codes with can help you
CodeProject:1
CodeProject:2
CodeProject:3
StackOverFlow
IF anything let me know.

Prevent ContextMenu on ListViewItem from disappearing when changing Filter

I have a ListView and TextBox for entering search text.
When the user searches for some text, I start a long-running search operation (on the UI thread, through a UI-thread-timer), and every now and again, while the search is still active, I change the ICollectionView.Filter property, to cause the ListView to refresh and present more items that the search has found as matching.
The problem is that if the user right clicks on one of the items, and then as Search is happening in the background, the tree is refreshed, then the ContextMenu disappears.
How can I prevent that from happening?
Here's my theory:
Because the whole tree is refreshed, WPF cannot know that the item (which was right-clicked) is the exact same item as after the refresh. Technically, that Item isn't even "there" anymore. It's really a new item with the exact same properties (from the ListView's perspective).
There are a some ways you can try to remediate this:
Capture the right-click of the item, store the item; capture the tree refresh, check if the item is equal to the stored item; if equal show the ContextMenu.
Instead of refreshing the whole tree, add items to the end of the list.
While ContextMenu is open, suspend refreshing the tree.

Pattern / Methodology For Dynamic ContextMenu Based on Collection of Objects

Background
I have a TreeView that follows the MVVM design pattern and supports multiple selection by recording TreeViewItem selections in a List. As it stands there are several types of TreeViewItems available for the user to select.
They are:
Two Root nodes of type WorldFolder and MyDataFodler which can contain child Folder types
Child Folder nodes of types LocationFolder, PersonFolder, CollectionFolder
Child Item nodes of type LocationItem, PersonItem
CollectionFolder can contain child nodes of Folder types
In all this works very well with very little code and supports collections of Locations and People and furthermore Collections within Collections.
Problem / Question
My top level view-model keeps track of the selection state of TreeViewItems and the current selection may be a combination of Item, Folder or even Root type nodes. Depending on the user's selection I want to create a dynamic ContextMenu. So far this works! When I select several LocationItem and/or PersonItem type nodes my view-model generates a custom ContextMenu. The problem is the complexity! My view-model is quickly turning into dozens of if/else if/else statements to capture all the possible permutations!
For example:
if (_selectedItems.All(item => item is PersonItem)) // Only people selected
{
// Create ContextMenu based on only PersonItems
}
else if( _selectedItems.All(item => item is LocationItem)) // Only Locations
{
// Create ContextMenu based only on LocationItems
}
...
Is there a better way to handle all the possible permutations of user choices and generate my ContextMenus more efficiently?
* Sorry about the code formatting, it's been giving me grief all week *
I can't remember where I read this wise saying:
The best way to work with a TreeView is not not to work with the TreeView
What does this mean? Move the functionality into the tree nodes and keep the tree view as thumb as possible. Unfortunately, by default the tree node does not many events, though, it's easy to redirect the tree view events to the nodes.
Once done, you can override the ContextMenuStrip property in your nodes. The first selected node creates the list of ToolStripItems it wants to handle and asks the tree view which are allowed (e.g. with a FilterMenuItems(desiredItems) method). The tree view asks all selected nodes which of the nodes they would be able to handle. The result is your context menu.
This should work with almost any count of different nodes and keeps the tree (nodes) easy to maintain.
Edit: Dang! Missed the WPF tag, so I cannot asses the available events, since I did not yet work with WPF

Categories