I'm building a Windows Store app using XAML. I have a scrollable ListView that has its SelectedItem set on start.
Now, I have two requirements:
I would like the selected item to be always visible
In addition it
should be displayed in the middle of the list when possible (it's
not possible for the first element).
The first requirement I can partially fulfill by using list's LayoutUpdated event and list.ScrollIntoView(item) method, but this doesn't allow me to scroll down the list manually until the item is unselected.
For the 2nd requirement I have no ideas.
Ok, found out how to achieve that: http://fczaja.blogspot.com/2013/05/xaml-listview-scroll-selecteditem-to.html
Related
I have a big list (~10000 items) inside a ComboBox, that uses the VirtualizingStackPanel class. The scrolling performance is good, anyway I like to know how to identify the top item shown in the GUI - which is, not necessarily the one with index zero, but the one that is on top of the current scrolled item list. ? Thank you.
I use the FrameworkElement.IsVisible property. Using the method at the link below, you can also check to see if an element is partially visible(which would be useful in your case):
In WPF, how can I determine whether a control is visible to the user?
You could (by using a background thread or by doing a computation when the user scrolls in the combobox), check the items in the ComboBox to see if their FrameworkElement.IsVisible property were set to true. If so, you update that elements IsVisible property in its ViewModel. Now you have a collection in your ViewModel of items that are marked visible or not(and that are constantly updated concerning the visibility of the CoboBox item that it represents). Now you can find which is the first using .First(x=>x.IsVisible==true) on the collection of items.
I got a treeview with a bunch of nodes where more gets added over time. When I add a new node to the treeview, I need to make sure that the very bottom most node is visible.
I tried using the ItemContainerGenerator to select the last item and bring it into view. But it doesn't work for me.
How can I make my treeview scroll to the very last item?
If you are working with TreeViewItem in code, I think you can use treeViewItem.BringIntoView() which should cause the scrollable area the item is in to scroll so that it is visible.
Edits
Examples
view.SchemaUpdateGrid.ScrollIntoView(e.UserState);
The above like is from a BackgroundWorker.ProgressChanged event handler. e.UserState contains the currently working object (custom class), not a grid row. The grid automatically translates the object into the item.
Problem:
I'm having trouble managing focus and item selection in a WPF ListBox with an embedded ListBox and some custom selection management in the code behind.
Background:
The "parent" ListBox is assigned a DataTemplate containing a "child" ListBox. I have added code to navigate using the up and down arrow keys from the parent ListBoxItem to the child items and from the child items back to the parent items.
When navigating back to a parent item which is not the first item in the first level list box the focus is always set to the first item and it is selected even though it is set in the program to a non-first item (e.g. parent item #2). I have traced through the code and the focus and selection is set to a non-first item but then another event to select and set the focus to the first item is always received.
There was another case on stackoverflow which was similar to what I am experiencing. The case is wpf listview lost the focus I tried all of the answers and responses in this case and none of them worked.
Does anyone have insight into why I can't programmatically select a non-first element in the parent ListBox?
Try using ListBox.IsSynchronizedWithCurrentItem = true then working with the list's collection view (if you're not explicitly creating one, you can use CollectionViewSource.GetDefaultView) to set the current item (ICollectionView.MoveCurrentTo()).
Selectors have a much more preferable method of dealing with selected items when working with the collection view, rather than SelectedItem.
I have been struggling with the Focus in ListBoxes too. Perhaps the answers to these two questions will help you further. Especially the trick with setting the focus on a Background thread is very very useful.
Deselect item in ListBox
Delay change of focus
Is there any way to tell when the containers are finished being made for a ListView?
A detailed explanation of what I've done so far
I have a ListView control that has a DataTemplate in one of its columns that contains a CheckBox Control.. I've figured out how to access the CheckBox dynamically using the object that the ListView is bound to.
ListViewItem lItem = (ListViewItem)ListView.ItemContainerGenerator.ContainerFromItem(trackToHandle);
CheckBox checkBoxToHandle = FindChild<CheckBox>(lItem, "CheckBox");
The problem is that the CheckBoxes "reset" (become unchecked) whenever I scroll too far or whenever I sort the columns.
I figured out this was because the VirtualizingStackPanel was only spitting out containers for those ListViewItems that were visible (or almost visible)..
And because the CheckBox is inside a DataTemplate that is defined in the XAML it gets thrown away everytime it goes out of view or when the list is sorted.
I got around this by creating a separate list of CheckBoxes and using the actual CheckBoxes "click" event to change the state of the corresponding CheckBox in my list.. then made a little method to go change the state of all the visible CheckBoxes whenever the user scrolls... as a result it appears like it should have in the first place.
Except when I sort the columns.
I tried making it re-do the CheckBoxes (like before) right after it'd sorted a column but it didn't work.
My best guess is that it doesn't immediately make the containers after I sort..
Is there any way to tell when the containers are finished being made for a ListView?
If you bind your checkboxes IsChecked property to a boolean property on your data context, then you will not have this issue.
The whole purpose of the VirtualizingStackPanel is reduce memory usage by not creating ListItem's unless needed.
In effect, you need to move the data side of the checkbox away from the control.
I've got a list of things that I want the user to easily edit the order of. Right now I'm binding them to a ListBox with a custom ItemTemplate. Within that template I have an UP & DOWN button. My goal was to move the item up/down based on the button clicked.
However I'm having trouble associating the button's click event with the actual item in the list. I've seen some folks setup a drag/drop for ordering items but that that would be too complex for this app (it's target user base =1, just me).
I assume this is possible with a ListBox. Maybe someone has a beter idea on how to implement this? It's not a huge set of data... less than 25 items.
The DataContext of the button (of the entire template, thus inherited by the button) is the item itself. Simply cast it to your desired type.