We are maintaining a List data structure. After list is prepared, we are assigning datasource to Treeview. We are applying 'Virtualization' property to 'True'. Initial structure is like below. After virtualization, visual portion is 53 items.
-Doc
-Chap1
Page1
Page2
...
Page1000
Then we are trying to add one more item called "Chap2" with 500 sub items ("Pages") to "Doc" node at run time and then setting vertical scroll position to last sub item.
Problem: We are getting blank treeview after this operation.
Please suggest that do we need to change any properties or anything we need to do?
Related
I would like to generate combobox items dynamically during run time. I have a combobox called Camera and say if I have 10 cameras connected then I want to name each camera as "Camera_0", "Camera_1", "Camera_2"..., and "Camera_9" and display them in the combo box.
I want to leverage a for loop to append a string to store each camera string name in an array so that I can display in the combobox.
Thanks
Build an array of strings first and then write to the "Strings[]" property of the combo box using a reference or a property node.
Edit: original snippet was wonky due to the property node. Just wire your combo box reference to the "Combo Box" node there and you're all set.
I do have a listbox in my WPF application. When the user has scrolled to the top and a new item arrives (it's an ObversableCollection) the new one is shown as new top one.
Can I change the behaviour in the way that when a new item arrives the scroll position is not changed meaning the new one is out of sight above the original top item.
It looks like you insert new items at the top of you ObservbleCollection so they show up on top of listbox. If you add them to the end of source, when they will show up at the end of ListBox. Or in case you have some sorting (via CollectionViewSource for example) then you need to modify you sorting logic to show added items at the end.
There is a ScrollIntoView method but it is not going to preserve the position only that it is somewhere in the list. If the item is below it is put in the last position. So if you put the "proper" last time into view to push the fist off as if the item is below it bring it to the last position. I have a simple list of 3 and I made the ListView Height=40 so it only had room for 2. The following pushed the first item out of view.
lvKDNames.ScrollIntoView(liIDName[2]);
If Height=Auto is will be more difficult to determine the proper last item but this is a possible approach.
I appologize for the novel but I wanted to explain as much as I have done thus far.
Within my current project I have an application that consumes a service that provides a collection as a <List>. Due to how I am using this data in the application I have had to convert this data to an observable collection. This was done so that as the data was selected and moved about the application UI updates would be refreshed using INotifyPropertyChanged and INotifyCollectionChanged.
Where I am having a challenge now is I have a listbox that is bound to the observable collection within the listbox I have a datatemplate that renders out the items of the collection. This data template contains a button which needs to allow the user to click the button for each item to remove them from the collection.
The use case for this is a listbox that stores selected name as chosen from a gridview. Once the user has selected names from the gridview they are stored ( within the observable collection as a queue) and rendered out in the UI in a listbox control which shows all selected names. I need to provide the user with the ability to remove these names in any order selected.
From what I have been reading there is no means to enumerate / index an observable collection. For situations such as this you should use List or an Array. However in order for the items to refresh in the list view they need to be in an Observable Collection.
From what I have read it appears that when the event is triggered I need to convert the observable collection to an Array and then evaluate the array to determine the index and then remove the record accordingly?
I think I may be off base on this as it seems like I am over engineering this problem? The above scenario does not seem correct is because I fell as if I am doing a lot of converting to and from the collections to just remove a record?
Does anyone know of an efficient means to remove records from a collection ( in any order selected) when the collection is rendered out as an items control within a listbox?
I’ve been successful in removing the last record added to the collection using RemoveAt() however I have not had any success in randomly removing records.
Afterthought: Part of this issue could be related to the fact that I have a button inserted within the datatemplate (control item) and as a result the item is not actually being selected before the event is fired on the button event?
Sorry for the rambling on this but I have had my head in this for hours and made minor progress. Any tips or ideas would be appreciated!
ObservableCollection<T> inherits from Collection<T> which implements IList<T>, so you can certainly index and enumerate it. It has a Remove method that takes the object to remove and removes the first occurrence in the collection and a RemoveAt method that takes an index and removes the item at that index.
Based on your afterthought, it sounds like you have a WPF ListBox with an ItemTemplate that creates a Button. ListBox will set the DataContext of each instantiated template to the item in the list being bound to, so you can get a reference to the item that created a Button from the DataContext property on the Button or by using a Binding.
I'm using a TreeView to let the user navigate a complex data structure more easily. I'm trying to add a feature to my application so my users can add new items to the datastucture by clicking a button on a toolbar. This new item has 3 levels, each with 1 item. I would like to select the item in the lowest level.
Adding the data isn't a problem, I just add a new item to the collection that is bound to the TreeView in a specific. I can lookup the item by hand browsing the TreeView, so I know the adding works. Now, I want to set the selection of the new item programmaticly. So the user can change the default settings in the element right away.
I've done some testing and I've found that setting the selection is done with something like:
var obj = TreeView.ItemContainerGenerator
.ContainerFromItem(selectedObject) as TreeViewItem;
obj.IsSelected = true;
I've tried adding this code directly after my Add-method. The adding function returns the new object and places this in selectedObject. The Add-method adds a to an ObservableCollection, which raises the appropriate events.
But, obj is always null directly after adding.
I've tried setting the selection in the LayoutUpdated event, but in this case the obj variable from the earlier code always null again.
I think I might be missing something here. Does anyone have an idea on how to add a new item to the bounded collection and select that item in the TreeView?
You might want to read this article by Josh Smith on using the treeview in WPF. He demonstrates how to use an IsSelected property that could easily be adapted for your needs, using the MVVM pattern.
Basically what I want to do is allow a user to type in a string value and have the list box scroll to the item that matches the text they have typed (or the first LIKE match).
If I use the .ScrollIntoView() method, the problem is that it puts the item at teh bottom of the visible area if the item is further down in the list than the current scroll position, and it is at the top if it is higher in the list than the current scroll position. I want to make it consistent by making it the top item in the list (unless of course it can't be due to being one of the last "page" of items).
I have tried to fake it by selecting the item which is x further down in the list where x is the number of items visible. This works when going down but breaks when going back up. and I've been unable to determine which index is currently the one at the top visible spot in my list.
WinForms list boxes have the .TopIndex property which des exactly what I'm looking for, but I've been unable to find the WPF equivalent. Anyone out there have an idea how to accomplish this?
Thanks in advance!
Use ScrollIntoView twice, first to show the very last item in the list, then to show your selected item. This way it will always be working from the bottom up. You'll need to call UpdateLayout after each call to make sure the positions are correct.