LongListSelector automatically at bottom - c#

So I'm getting a weird behavior with the native LongListSelector from WP8.
It's ItemSource is bound to an
ObservableCollection<Group<Something>>
Everything is displayed correctly, but when the list appears on screen, I'm at the bottom of the list instead of it's top.
The only thing I do is to fill the ObservableCollection via it's Add() Method.
Is this a known behavior or is there a workaround ?

It is a normal behaviour as you are adding items one-by-one. for required behaviour kindly feed the list and then set it to observable collection

Related

Binding values from local array to specific property (Visibility) of all listbox items

I need to change Visibility of all items in listbox in code based on changing some filters. I tried to loop the items using
myListbox.ContainerFromIndex(i)
but only first a few didn't return null. The answer why I quickly found here:
Why ItemContainerGenerator.ContainerFromIndex() returns null and how to avoid this behavior?
Turning off virtualization doesn't look like respectable solution to me, also I don't feel well about scrolling every item to view before calling ContainerFromIndex, even if it could probably work.
I'm not very clever from the rest of topics dealing with bindings and dependency properties etc, maybe also because majority of them are written in xaml and not in code. But I'm pretty sure the answer should be there. I'm very new to c# and I didn't find any clear-cut solution to such a basic problem.
I will prepare something like this array of Visibilities
Visibility[] visibilities=new Visibility[100]
...
and I want myListBox with 100 items to use these values. Either automatically with some c# magic or in a cycle as I used to do in Win32.
Based on the comment of Depechie, I made a research targeted on collections and created a functional code. Here is what I did:
Instead of adding items directly to listbox by
lbGames.Items.Add(...)
I created two collections:
List<object> gameList=new List<object>();
ObservableCollection<object> filteredGameList = new ObservableCollection<object>();
Adding items this way:
gameList.Add(...); or filteredGameList.Add(...)
While gameList is holding all items (stack panels), filteredGameList is holding only those I want visible. I had some problems with using only one big collection using its Visibility property, maybe my mistake, maybe not.
Withouth ObservableCollection I wasn't able to redraw listbox, that was problem with another List<> holding filtered data. When I filter data, I first clear filteredGameList and then I add all items I want visible, never changing Visibility property. ObservableCollection always tells listbox when items change.
On the application init, I attach listbox to this filteredGamesList.
lbGames.ItemsSource = filteredGameList;
Now everything works fine.

ObservableCollection - removing items by looping through vs .Clear()

I have an ObservableCollection which is bound to some ItemsControl (FlipView) as its ItemsSource. I was having an issue with the scrolling on a touch device where the entire FlipView would disappear as soon as user touches the screen and it would reappear when the finger is removed from the screen. So to fix that, I had to work around it by clearing the collection whenever I had to refresh it (instead of re-instantiating it). I noticed that the same issue (with touch) was occurring when I used .Clear() in order to clear the collection but when I looped through the collection and removed the items one-by-one the issue was resolved.
So, I am still not clear as to what is the difference b/w these two ways of clearing the ObservableCollection?
If I remember correctly internally the ObservableCollection maintains a IList which is assigned an instance of List. The ObservableCollection.Clear method eventually calls Clear on the interal list and that List.Clear method then uses Array.Clear to clear the items.

Xaml ListView strange behavior (Windows Phone 8.1)

I am working on Windows Phone App (Windows Phone 8.1) and I have a strange behavior of my ListView control.
I have a ListView with text items (Sort of a chat) which is bound to ReadOnlyObservebleCollection to whom I am inserting items as I write a new message or when I receive a new one. I choose my ListView items template with my DataTemplateSelector.
The issue:
Sometimes, when my ListView is empty, I add a new message to my ListView I don't see it on the screen, but I can see that it in my collection and I know for sure that the RaisePropertyEvent is fired.
As I continue to add new item to the collection I can scroll down the list (there is some sort of placeholder in the first idex) and then I am seeing the rest of the items - except the first one.
I've also noticed that the missing item is getting to the DataTemplateSelector and it's returns the right template.
Getting weirder, when I am refreshing the view - all the items(including the first one) at their right places.
I guess there is something wrong with the first item generation but I don't get any binding exceptions or such.
Any help will be appreciated.
Same issue was occurring to me in my ListView. It was happening because listview was not loaded(loaded event was not fired) at the time first item was added to my observable collection. To fix this issue I start adding items in my observable collection after listview loaded event is raised.

WPF Combobox - DataBind Empty Space Issue

I have a simple combobox bound to a List where A has a property Key and a property Value.
The combobox binds fine and works except for one flaw. It has a large empty space at the bottom where there are no items (i.e they don't get highlighted on hover or any such thing, there are no extra items, it is just that there is an empty space). How can I get rid of it?
http://i.stack.imgur.com/2yN9r.jpg
This is most-probably because of the template of the ComboBox under your current theme. Try changing Windows theme to Aero or Luna (seems like now you have it set to Classic).
If that's the issue, then there's a way to "fix" it by using a custom template, but then you break consistency from user's point of view. You'd need to take it into consideration.
you need to change the Template of combobox and reset the popup height acordingly. check below.
http://msdn.microsoft.com/en-in/library/ms752094%28v=vs.85%29.aspx
Search for popup under the tempalte and add the minheight=0 into it.
In my ComboBox ItemsSource="{Binding MyItems..., if MyItems is a List, it will leave extra space. if MyItems is an ObservableCollection, the extra space goes away. This may due to a Microsoft bug that ItemsSource was not properly notify by MyItems change when it is a List object.

WP7 Panorama items binding and adding other custom panorama items c#

I am running into an issue where i am using a panorama control and binding it to a datasource. But i still do want other custom items on another panorama items where i need a textblock, a grid and so on. So if i am adding it in the backend it doesnt show up those panorama items. It just shows the datasource binded items. Why is that so? Both of them should work out.
Can anybody help me with a solution for this.
Thank You.
Since you're wanting to manually add PanoramaItems, I can think of two approaches:
Make sure your Panorama.ItemsSource is set to an ObservableCollection that is accessible in the code behind or viewmodel, and then add your new items to that ObservableCollection which should update the Panorama.
Don't databind the Panorama control's Items - just add items manually when you want them.
Either way, the Panorama's ItemTemplate gets evaluated when the items are added to the underlying collection, so using a DataTemplateSelector will allow your code to determine which DataTemplate to apply when the new item is added without affecting the templates for previous items.
/chris

Categories