WPF: Listbox, centering selected item - wpf

Is it possible to always keep selected item in the middle of a listbox? If the user selects an item, I want to scroll so that the newly selected item is in the middle.
I guess it want be possible for the 'edge cases' (the first and last few items), but that's ok.

David Anson posted some articles on his blog that might help you here: Part 1 and Part 2. He gives an extension method that centers an item in an List Box. You might be able to build on that.

It is possible with couple of lines of custom code. Here's a discussion of a sample implementation. You might need to tweak it a bit to account for even number of items, insted of only odd number, but the general idea is the same.

Related

Nested ListView Items - TreeView&ListView combined

The best way to show what I need is showing an image:
What is the easiest way to implement this kind of "nested ListView" - each Item can be expanded and has subitems. I want the hierarchy only to show on the first column as shown when any other solution can be accepted.
There are couple of ways to do it. I consider that you are good in WPF and easily check code and take idea from it because it is not easy to give code of your requirement here.
You must need to user Expander for expand and collapse functionality for the detail sections as per you image.
I am sharing some the links from where you can easily get what you want. It is not exact code which you can copy but it will give you basic structure of what you want.
Listview and Expander
Sample-1
Expander and DataGrid
Sample-1
Sample-2
You will enjoy working on this one.

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.

Checkbox in List View ABAP

I want to do a simple thing but I don't have any idea. I made a button in GUI status it has a functon key and it works. Now the real thing. I want that for every checkbox I mark, when I press the button to call a new screen to show me only the entries that were checked.
The checkbox is added in the itable, but I think it doesn't matter that much. How can I tell the button which checkboxes on screen where checked?
The answer is pretty much what Bernard said, though it is lacking some detail.
You will need to know how many lines you wrote, this is stored in SY-LINNO, note that you need to also take into account how many pages you outputted which is stored in SY-PAGNO and how many lines are on a page which is stored in SY-LINCT.
You could get that info, which is probably a better approach, with 'DESCRIBE LIST'.
Then for each line with the command 'READ LINE' you can find the value of the checkbox.
READ LINE line OF PAGE page [result].
From there things should be a piece of cake.
For the checked records the checkbox field is 'X', while for the unchecked records the checkbox field is space (' '). So you need to loop over the records on the screen, then only display the ones where the checkbox is equal 'X'.
Besides all the answers you already got, you don't want to dig into interactive list programming any deeper (unless you have to do this for a programming exercise). Take a look at the ALV Object Model Documentation and the SALV_DEMO_* example programs.

WPF: Getting a list box to stay put

I have this situation where I have a ListBox which is being populated from a background thread (it's an address book and the data is coming from AD).
The problem is that since the list is sorted (using CollectionViewSource) and also available to the user while more data is being retrieved, it's bouncing all over the place as new items are being inserted at various places in the list. So it's available to the user, but mostly unusable since the user's selections keep going out of view.
Is there a way to keep Focus to the item selected, and preserve the selection, even if items are being inserted above and below the selection from the background thread? I would prefer not to sort on the server, which I understand can be a bad thing when it comes to AD.
I'm going to respond to this from a UI design perspective rather then a technical code perspective. (I'm sure someone else will have a way to have the list box keep the selected item in view)
I would argue that the use of a list box while large ammounts of data is being added to it is fairly impossible to do nicely. Lets just say you do get it to keep the selection in view, what about while the user is still searching for their required item, you wouldn't be able to keep it still then.
Firstly if the expected total load time is under 10 seconds you could just disable the list box until loading is finished. (Obviously grey it out with a spinning animation or something so the user can see it's doing something.) I'm assuming you have already dismissed this option otherwise you probably wouldn't be asking here. But I do think this is worth considering. If the load time is farily small consider if your users will really gain anything by being able to browse the list while it is still being loaded.
Secondly, I would propose that you find a way to restrict the contents of the list box so that only small quantities are every displayed at one time. You could do this by only displaying names starting with a single letter of the alphabet (along with a letter selection control). Or you could provide a filter entry text box at the top where the user could type the first few letters and the list box would only display the names starting with those letters. This would allow the user to type say "sa" and the list box would display "sam", "samantha", "sacha", etc. Now you only have a small number of items in the list so you don't have to worry about it jumping around. If the number of items in the list grows too large (because of the loading on the background thread) and the list goes beyond the height of the box the user can simply type an extra letter to futher filter the list.
Sorry if this isn't really what you wanted, but I thought it would be worth bringing alternative design up incase you'd overlooked it.

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