I use listbox.ScrollIntoView(item), but as long as this item is in listbox view, it won't scroll.
Actually I want the item to be centered in the listbox's view. Is there anyway to do this?
Unfortunately this is not all that easy, it depends on whether your listbox is virtualized or not, which depends on the panel that it uses to render its contents. For a non-virtualizing listbox, you can set the vertical scroll position by invoking the following method on the ScrollViewer which is part of the listbox template, where offset is in pixels.
ScrollViewer.ScrollToVerticalOffset(offset);
For virtualizing, you can invoke the following method on the VirtualizingStackPanel, where offset is the list location (you can use a double value, i.e. scrolling to 3.5 will scroll to half way between index 3 and 4):
ItemsHostStackPanel.SetVerticalOffset(offset);
For how to use this code in context, look at the jump list control I implemented here:
http://wp7contrib.codeplex.com/SourceControl/changeset/view/72741#1502048
Related
As the title say:
I want to know values of a scrollbar in the ListView such as:
Minimum value I know = 0 :)
Maximum value of a scrollbar
Current value (position of a bar inside of a scrollbar)
Is this possible?
How to make it work for both iOS and Android?
I have tried with ItemAppear to get the position in the ListView but all I can get is the position of an item not the actual scrollbar position and with ItemAppear the problem is if I have big images in the list then I scroll 1 mile but nothing happens until the other item appears.
ListView doesn't fire Scrolled events like ScrollView does.
If you still want to get current value or maximum value of scrollbar, I suggest you can render new Listview, that contains scrolled event. or you can search some third party control, may be have this event.
I am new to Windows Phone Programming. In my application, I Have a listbox which lists the phone contacts. Currently it is listed as a regular list with equal size for list items in the UI.I am looking to modify the front end like this :
I dont want to have different sizes /back ground color for each list items rather a fixed UI and let the lists scroll through it and the list item in the view, at any time, should be displayed as in the picture.
I dont expect any codes as answers but any examples are welcome too, just want to know using what feature this kind of functionality is possible so that i can do my reading!
Thanks,
Deepak
It would be difficult to modify an existing control (ListBox for example) to act like this, so your best bet would probably be an ItemsControl with its RenderTransform set as a TranslateTransform.
If you place a Rectangle (with Fill="Transparent") over the ItemsControl, you can attach handlers to the ManipulationStarted, ManipulationDelta, and ManipulationCompleted events to control the "scrolling" by setting the TranslateTransform's Y offset.
To resize the items in the list there are two options: a custom panel or manual setting.
Custom Panel
You could create a custom Panel implementation that will appropriately resize its Children based on a property you would create to represent the scroll position. Set the ItemsControl to use your panel, and either through binding or attaching a handler to the panel's Loaded event and keeping a pointer to the panel, update the aforementioned property from inside the ManipulationDelta handler.
Manual Setting
From inside of the ManipulationDelta handler, you can also calculate the various heights of the boxes and use MyItemsControl.ItemContainerGenerator.ContainerFromIndex to get the visual for each item and set its height.
I would suggest putting this all inside of a custom UserControl.
You may have issues with clipping using the TranslateTransform but hopefully this will get you started. Unfortunately, this looks like a rather difficult control to try making as your first windows phone project!
So Finally I did manage to find a way to do it.
First approach was as #Murkaeus suggested, Using UserControl and ManipulationDelta event handler. However for some reason the manipulationDelta event was triggered only for 2 finger gestures (Zoom, Pinch..etc), I have no clue why. And after some trying I had to give up on this.
The next approach was using Listbox itself. The source of the Listbox was set as the List( of Models objects) that I create after reading the contact information from phone. The height and color of the listbox item was bound to a property in my model named "scaleLevel" and was accordingly converted by implementing IValueConvertors to give predefined color and height values for different scale levels.
I created an attached property for the scrollviewer vertical offset like mentioned [here] (http://www.scottlogic.co.uk/blog/colin/2010/07/exposing-and-binding-to-a-silverlight-scrollviewer%E2%80%99s-scrollbars/)!
This event is triggered on change of the vertical offset and every time there is a scroll, I find out which listbox item to be enlarged and which reduced based current vertical offset.
Once I have this information, I change the property ("scaleLevel") of the affected items in the List (of Model) (which is bound to listbox height and color). This change is updated in UI using the INotifyPropertyChanged Event.
I have no idea if this the best way of doing it , but it works well and there is no considerable in updating the UI despite the processing involved.
I would like to hear your opinion about the implementation and any other solution which you feel will work better.
I have a Listbox that bind to an ObservableCollection. In my app, I need to clear this collection in OnNavigatedFrom and read it in OnNavigatedTo to reduced memory.
However, I see that when using StackPanel as ItemsPanelTemplate, Listbox can remember its last view (its seletected item), while VirtualizingStackPanel does not.
So how can I disable this behavior when using StackPanel ?
It is a common problem, you need to set the Listbox's SelectedIndex to -1
I have listbox in WP7 where i need to scroll to certain items according to user choice and i use scrolltoview for that. Problem is that i need listbox to scroll listbox enough so that selected item appears aligned to top edge. Right now scrolled item is positioned at bottom.
Assuming that you have fixed (and known) size items and a fixed (and known) size of listbox, can't you just account for the number of items between the top and bottom of the visible space and adjust your offset accordingly?
Unfortunately Matt solution was unusable in this case because listbox items were too big taking almost whole screen per item. But i was able to solve this problem by getting scrollviewer of listbox and use its scroll method to scroll into listbox SelectedIndex. Item is still not positioned perfectly after scrolling but height difference is minimal and acceptable for me.
I want to modify a stack panel, so when an item is removed from it all the items under it slides in animation upwards to fill the missing gap. what is the approach I need to take in order to achieve this ?
Use ListBox instead, and have its ItemsPanel be a StackPanel. Then, modify the style of the items to include an animation in AfterLoaded, BeforeLoaded, and BeforeUnloaded visual states.
http://msdn.microsoft.com/en-us/magazine/ff798276.aspx