WinForms TreeView: Treeline hide after a certain level - winforms

I would like to know if it is possible to hide treeline in a treeview of winforms after a certain level.
For example to have node1 connected with child node2 with treeline and node2 has a child without treeline

Related

Draw connector between MDI children in Windows Form

In Windows Forms, I would like to show relationships using lines to show a connection between two forms inside an MDI parent similar to MS Access' relationship diagram.
What are my options for drawing lines underneath the children (or on parent background)?
If you are going to use MDI then what you would need to do is handle the Paint event of the MdiClient control that hosts the child forms. It's not actually exposed directly but you can access is via the Controls collection of the parent form. It will be the only child control of that type and, in most cases, the only child control. You can then use GDI+ to draw lines between the appropriate pairs of forms.

WPF add/edit/delete treeview nodes in MVVM style

I am new to MVVM and WPF treeview. I have done some research and have read the Josh Smith article on MVVM, and this, and this.
I think I have no problem creating the treeview in WPF. The thing is on my app, the left panel is the tree view, the right panel will display some properties of the selected tree view node, which user can click a button to edit the properties and save it to the data source (and potentially will affect the treeview item). In addition, user would be able to add/delete child node/grandchild node.
I can't seem to find any article/example to implement this using MVVM.
I am currently thinking that in the view models for the child node and grandchild node, I shall add a public property which points to a Usercontrol. The right panel will bind to the treeview's selected item's Usercontrol. The thing is, when user adds a child node/grand child node, the right panel will be used to let user fill in information and save. I am not sure whether it will affect the binding.
And other issues like editing the properties of a tree node will mean to copy all the child node info of the node to the new node and delete the old node from the tree and add the new node to the tree?
Can someone point me to any good articles on a similar implementation or give a rough idea on the issue that I should take note etc?
Thank you very much.
Angela
A lot depends on your setup but here is a way I have used before.
Note that you might need a ChildPropertyChanged event type of thing (I made that name up) to bubble up changes in the tree to the root of the tree.
To add a node
I created a ViewModel that contains:
a property for the tree data collection (probably a reference to the root node)
a property called NewNode.
a property called CurrentNode
a command that adds the NewNode to the CurrentNode: AddCommand
In the View:
Bind the TreeView to the tree data collection
Bind the SelectedItem of the TreeView to the CurrentNode
Bind the controls with the data for the new node to the NewNode properties
Bind a button to the AddCommand
In the AddCommand:
Add the NewNode to the CurrentNode and re-initialize the NewNode property.
To edit a node
In the ViewModel
add a command: UpdateCommand
add a command: EditCommand
In the View
bind some edit controls to the CurrentNode's properties (OneWay binding)
bind a button to the EditCommand
bind a button to the UpdateCommand
In the EditCommand:
make the edit controls and update button visible (I use a State property to bind to, see Extra below)
In the UpdateCommand:
write the values of the edit controls to the SelectedNode
hide the edit controls (I use a State property to bind to, see Extra below)
To delete a node
In the Viewmodel
add a DeleteCommand
In the DeleteCommand:
remove the CurrentNode from the collection
Extra
I have found it very useful to implement IEditableObject on the ViewModel of a Node.
Using the methods of this interface you can add a cancel button to reverse the EditCommand. By adding a State property to the ViewModel of a Node (New, Modified, Deleted) you can track changes, know what updates to send to the model/database and you can bind the View to it to show/hide elements.

How can I create an always-expanded treeview?

Well, I thought I had this one solved. I simply changed the TreeViewItem's template to ignore whether a particular node was expanded or not. Looks great! But that's just it... it looks great! Keyboard navigation still responds as if the node was expanded or collapsed.
For instance, if I'm on the root node and hit the right arrow (nothing moves, nor should it) then hit down, I go to the first child of the root. However, if I'm on the root node and hit left (again, nothing moves) then hit down, I jump to the second root node, jumping over all the first node's children!
Needless to say that's not the behavior we want. We could simply swallow the left and right arrow keys, or simply abandon the treeview entirely and move to nested items presenters (which is sort of what a TreeView does anyway) but I'm hoping I don't have to re-create an entire control just for this functionality. Thoughts?
In your TreeViewItem template, you can set IsExpanded to true, and all of your items should then be expanded if they have child elements.
Unfortunately you may have to swallow those keystrokes to get the keyboard navigation you want, as the navigation you are seeing is by design.
You may be able to create a custom treeview inherited from TreeView and put those button events in there so that you can re-use it.

How to access inner TreeViewItem nodes without expanding the parent node

I have a WPF TreeView that is bound to a collection of custom objects - each of which has an IsExpanded property to expand the container TreeViewItem. I want to be able to programmatically access each TreeViewItem's control contents without expanding the parent TreeViewItem in the visual tree. However, when I attempt to access said items without expanding their parent it seems as if they have not been added to the visual tree yet. Does anyone know a work around for this?

Programmatically show expand arrow on Silverlight TreeView

I have a Silverlight TreeView control that I'm dynamically populating when a specific node is clicked. For example, I load the first generation (level) of nodes, then when the user clicks on one of the nodes, I use the Selected event to populate that node's children, etc, etc. I'm sending back from my database a bool value with each node value that indicates whether or not the new nodes have children nodes.
I am wanting to set the expand arrow to show in front of new nodes that have a child, but the catch is that its children will not be populated yet. I thought maybe setting the HasChild to my bool value, but HasChild is read-only. Any suggestions would be very much appreciated.
Just a add blank treeviewitem when it's collapsed just to show the arrow.
But when expanded by the user, clear that blank child, and do an async call to get the real children.
Set the UserState to the parent treeviewitem or id when doing the GetChildrenAsync call.
When they arrive, the UserState should be that parent treeviewitem or id, when you find that parent which you just set it's itemsource to the e.Result.
If you're familiar with dependency properties, you could create a dependency property that would be settable by you in code and gettable in the xaml so it could be bound to.
This would allow you to use a ValueConverter to render the visibility of the arrow.

Resources