Add a treeview item and restore tree status - c#

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!

Related

Prevent automatic selection of the primary TreeView node

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?

Remove item from listbox in XAML

I have a listbox with items in my xaml page. I introduce those items programmatically (I don't know if this is important). I want to delete them as you do in an app like uwp mail.
I want that whenever you click over the left side, a checkbox appears and a button to send to trash those items. Is this a kind of components or on the contrary I have to code it?
If you want to remove the items in a list box, you can do it like this:
listBox1.Items.Clear();
If you want to activate a checkbox in XAML you can add it in the designer, set visible to false. Change it to true in your code behind when you want to activate it and use this to remove a selected item:
listBox1.Items.Remove(item);
You may need to iterate through all the items and check if they are selected.

How to update ListBox control with Dictionary binded to it without jumping to the first item

Is there any way to update a ListBox control with Dictionary binded to it without resetting its DataSource property on every dictionary change? The reason I don't like this solution is that it forces a ListBox control to jump to the first item as mentioned in other questions like this.
Here is a minimal example that reproduces this behavior -- https://bitbucket.org/ntrophimov/updating_issue (about 20 lines of code to read in the MainForm.cs file)
Is there any other solution for this problem?
Is there any dictionary implementation in which I can manipulate items (add and remove them) and these changes will be immediately represented in a ListBox control without refreshing its whole content?
The idea is:
You show your ListBox with items
User click on some item
Applications save the 'selected' item in local variable
User/App initiate ListBox update process
Update process done -> application restore (re-assign) the 'selected' item (if it still valid for current items of ListBox)

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.

Freezing a listboxitem while items are being added

We have a ListBox that has a number of items. Items are inserted into the ListBox via an ObservableCollection. Some of these items can be edited right in the ListBox. However, if an item is added at an index < the edited item's index, the entire content of the ListBox moves down.
What we'd like to do is the following: if an item is in edit mode, we'd like to freeze its position on the screen. It is fine if items are added to the collection and the UI around the item changes. But the position of the item should remain constant on the screen.
The only thing I've been able to do so far is attach to the ScrollChanged event and, at most, use either BringIntoView or ScrollIntoView methods to ensure that the item is always displayed somewhere in the UI, but I am unable to lock down its position.
Has anyone done something like this and help out?
I think the following would solve your problem:
When entering edit mode, keep a reference to the object you're editing, its index in listbox and the ScrollViewer's HorizontalOffset.
In a handler to the ObservableCollection.CollectionChanged event find the new index of the object you editing, if the index changed, swap the edited item with the one now taking it's place (or some other logic if you want to keep a certain order). Then if the ScrollViewer.HorizontalOffset changed, move it back to the said offset.
This will make sure the item you're editing will always stay in the exact same place in the list and in the UI.
Hope this help you out.
You could always force items to be added to the end of your collection. Otherwise, I think you are on the right track with scrolling the ScrollViewer. When entering 'edit mode', track the current horizontal offset.
ScrollViewer sv = listBox.GetScrollViewer();
double indexPos = sv.HorizontalOffset;
However, I think you would be better off attaching to the ObservableCollection.CollectionChanged event. When the event fires, check if the new value was inserted above your 'edit mode' item. If it is, then run the following code.
ScrollViewer sv = listBox.GetScrollViewer();
sv.ScrollToHorizontalOffset(indexPos+1); // This will obviously require an offset
Is the problem that you simply don't want it moving? Or is the problem that you have referenced said item by its index number and the change in index causes other problems when you apply the changes (like applying them to the item that is now at the previous index)?
If the problem is that a change of index causes problems in code, then I would stop using the index and instead get a reference to the object. Later you can always get the index of the item if you find you need it.
Would it be feasible to switch to a ListView, and use EnsureVisible?
If freezing the rest of the listbox is a viable option. Such that items which are added don't appear until after the edit has been completed or escaped then you could try using BeginUpdate and EndUpdate to stop the ListBox from being repainted. I'm not sure how that would effect your editing process though.

Categories