How to avoid automatic display of new ListBox items - wpf

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.

Related

XamDataGrid sort in Reverse Order

I have xamdatagrid in which I Add Item, every time new item added it goes to the bottom. I want every time I add item it should be on the top row "like reverse sort"
also in field layout setting I am adding new record on top.
note: I removed sort option from field
Note: I dont want to sort by any field except by row insertion order if its in xamdatagrid.
or sort of list like list.reverse possibility in xamdatagrid.
Assuming you're binding your grid to an ObservableCollection, use the Insert(0, item) method.
You can also use a CollectionViewSource for your grid's data source and set the sort there, while calling Add(item) on your source collection normally.

Winforms Listview checkedItems missing

Basically, I have a listview inside my form. In order to make the process of selecting the different items in the listview quicker, i have to add a "select all items" checkbox.
For Each lvItem As ListViewItem In Me.lvwDatos.Items
lvItem .Checked = True
Next
That's about it, very simple. Once i click on the select all checkbox, i can see clearly how all the elements go into checked state. However, on the next step, when i want to loop through the selected items in my code and do whatever tasks should be applied to them, i'm finding that ALL elements are unchecked. What's making them loose their state?
Ok, nevermind, i found the problem...that's how it is supposed to be, there's no problem in the listview, it's just the chain of events that were taking place that broke it all...legazy code, as usual...
This is why I have designed Better ListView component which have this behavior fixed (and many other quirks of .NET ListView).
There is also a free Better ListView Express, if you are interested.
The checked item collection is maintained separately and you always get its actual state.

wpf Treeview blank while using virtualization

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?

How to obtain the selected item after Listview Binding?

I have a ListView and it's getting updated every 5 seconds.
The problem is that the selected item vanish. How can I obtain it ?
It depends what you're doing to update the list. If you're deleting and re-building the list willy-nilly (which isn't great), you'll need to store something which can uniquely identify the selected item before deleting the list and then use that stored value to select it again once the list is repopulated. A better solution is to use something like an ObservableCollection, and try not to wipe out anything in the list unless you really need to.

Is there a WPF ListBox equivalent to Windows Forms Listbox TopIndex?

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.

Resources