I have a WPF application that includes a TreeView. The user adds content to the tree using right click and context menu.
This creates a new TreeViewItem that is added to the tree's ItemCollection.
The problem is that sometimes the new items are not shown, although they were added. If you minimize/maximize the window they suddenly appear.
I tried to call TreeView.UpdateLayout() after the addition of the new item, but the result is the same.
Any suggestions?
dont add to the trees item collection. bind the treeview to an observable collection. keep the model and the view seperate. the treeview will reflect changes to the model. you should never have to call treeview.updateLayout. add your items to a collection. (your model) which is bound to the treeview. its a lot less work
I am not shure about this but try calling Refresh or call on the node witch is parent to the new node ExpandAll. Hope this works for you
Best Regards,
iordan
Related
Simplified version of the problem. So concentrate on the technical issue not user friendliness. ;)
I have a listbox filled with items from an ObservableCollection.
It is showed in my first view.
By selecting an item in the list the user goes to a another view.
I swap this view in, so the first one is not in the xaml tree.
In the second view they can perform actions.
One of the actions makes the item go away from the top list (like passing the item on to another user).
The first view listens for that event and can remove the item from the list.
Items.Remove(item)
Problem is that the item doesn't always disappear from the ListBox (but it does from the underlying list, I've checked with debug). I think it might have something to do with the view not being displayed at the moment the collection is updated. I've tested with setting SelectedItem to null, thinking that selection might keep it there after removal, but it made no difference.
Is this a known limitation, having to be visible for the listbox update to take effect?
Can I trigger something to make the observablecollection re-fire the notification?
The title of the question is bad, feel free to update it.
I have an observablecollection that generates a user control whenever it an item has been added to it.
However there are times where I just want to add to the collection and not have it create a usercontrol. Is there a way around this problem? Thanks.
Create a collection view around the observable collection and bind the UI to that. The collection view will have a filter so that only items you want in the UI are filtered in
I followed the following tutorial (answer) to create a treeview which is bound to datatase, the only exception being that I have my treeview in a user control.
Populate WinForms TreeView from DataTable
My problem is that after adding a new node or child node the tree doesn't get updated. I have tried to refresh, expandall etc the tree but still doesn't show the newly added node. Bear in mind it is in usercontrol and I'm adding new nodes from parent form.
You might try TreeView.BeginUpdate() and TreeView.EndUpdate() methods as well, before and after the responsible code for altering nodes to your treeview.
I am new to silverlight so bear with me.. i have a view model in which i have 3 ObservableCollections of 3 classes (inherited from INotificyPropertyChanged). One of this classes is "Favorites" . In a child pivot page(of the main panorama view), i modify the favorites (i add one to the database etc.).
How can i "update" the view of the main panorama view? is it sufficient to add the new favorite object to the ObservableCollection or do i need to implement some new actions? From what i understood, the views are active, so if i modify an element (even one linked though Binding) it should update. Is this true?
You don't need to do anything if you add or remove an item to an ObservableCollection, the UI will show this new item automatically. However, if you modify your underlying data object, the UI won't be updated unless your object have implemented INotifyPropertyChanged.
You can take a look at this post or this one.
I am trying to synchronize the selected tab item of a WPF tab control with the last item that was added.
Since there is no such property as e.g. IsSynchedWithLastAddedItem, I am trying to detect when an item was added in order to point the SelectedItem at the last added one.
I cannot find the event that gets raised - either on the tab control or its Items, when a TabItem was added.
I am sure something like it must exist though, so I hope someone can help me out.
var view=CollectionViewSource.GetDefaultView(m_tabControl.ItemsSource);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
If you work directly with the Items-collection, the same technique will work also. Get the default CollectionViewSource for this collection.
var view=CollectionViewSource.GetDefaultView(m_tabControl.Items);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
As Timores wrote, for the m_tabControl.Items-property, you can attach a handler directly. The same is also true for most ItemsSource-references, but there you have to check yourself for the INotifyCollectionChanged-interface.
I have not tested it. Make a comment if it does not work.
The Items collection is of type ItemCollection, which derives from CollectionView which implements INotifyCollectionChanged. So you could listen to CollectionChanged and get to know when an item is added.
Don't know how to do that in XAML, though.