How to select vertical ListView item based on its current position using XAML and C#? -
i'm trying tricky. want select list item determining whether scrollviewer
scrolled , if item in center of scrollviewer
. want select item when user manually scrolls center of listview. not want scroll selected item, because easy, right?
here's drawing:
i've managed observe whether scroller has been scrolled. first place loaded event on listview in question "itemslist" , find scrollviewer
type traversing visual tree see if has finished scrolling , tag scrollviewer
listview , handle selectionchanged event:
private void itemslist_onloaded(object sender, routedeventargs e) { var listview = (sender listview); if (listview != null) { var scrollers = findvisualchildren<scrollviewer>(sender dependencyobject); var scrollview = scrollers.first(); if (scrollview != null) { scrollview.tag = listview; scrollview.viewchanged += scrollviewonviewchanged; listview.selectionchanged += listview_selectionchanged; } } } public ienumerable<t> findvisualchildren<t>(dependencyobject depobj) t : dependencyobject { if (depobj != null) { (int = 0; < visualtreehelper.getchildrencount(depobj); i++) { dependencyobject kid = visualtreehelper.getchild(depobj, i); if (child != null && kid t) { yield homecoming (t)child; } foreach (t childofchild in findvisualchildren<t>(child)) { yield homecoming childofchild; } } } }
i handle scrollviewonviewchanged
event determine if scrollviewer still scrolling. i'm running few problems need determine position of listviewitem
, select - i'd love help here:
private void scrollviewonviewchanged(object sender, scrollviewerviewchangedeventargs e) { //debug.writeline("scrollviewonviewchanged + isintermediate="+e.isintermediate); var scrollviewer = sender scrollviewer; var listview = scrollviewer.tag listview; if (!e.isintermediate) { debug.writeline("isintermediate = false"); // todo: determine center item , set selected item group. } }
then selectionchanged
event create magic happen:
void listview_selectionchanged(object sender, selectionchangedeventargs e) { debug.writeline("selectionchanged"); }
thanks! help appreciated!
you can item in middle using method inputhittest
of scrollviewer
.
example:
iinputelement element = scrollviewer.inputhittest( new point(scrollviewer.width / 2, scrollviewer.height / 2));
c# xaml windows-8 winrt-xaml
No comments:
Post a Comment