Adding a new item to ListBox.ItemsSource - windows-phone-7

I'm developing a Windows Phone app.
I have a listbox with this C# source code:
System.Collections.Generic.IEnumerable<OpenGame> items;
...
GamesList.ItemsSource = items;
If I want to add a new item (a new OpenGame object) to the GameList, how can I do it?

Use ObservableCollection rather than IEnumerable then you can use the Add method.
IEnumerable is for static lists where you don't want to add or remove members.
ObservableCollection is designed for lists bound to ListBoxes and similar controls. As well as allowing you to add and remove items it will notify the list box so it gets updated when the collection changes.

I think you can find you're answer with explanations in this thread:
How can I add an item to a IEnumerable<T> collection?

Related

Listview is not updated after sorting (windows phone)

I have create a DataModel class which contains INotifyPropertyChanged method.
Then I assign ListView.ItemsSource to the Observable Collection of the DataModel.
If new item is added to the Collection, the ListView is updated correctly.
But when I try to sort the list:
IssuesList = new ObservableCollection<Issue>(IssuesList.OrderBy(x => x.name).ToList());
The ListView is not updated automatically (order not changed).
The ListView is only changed when I set ItemsSource for the listview again.
How can I make the ListView updates automatically when items order is changed?
Thank for your helps.
This happens because you're deteaching the instance. An observable collection only fires updates to the UI if its elements were modified but not when you're replacing its instance.
What you will have to do is to raise the propertychanged event after you replaced the collection with the new sorted one. Depending on how you implemented the notifying event:
NotifyPropertyChanged("IssuesList");

Windows Phone : Long List Multi Selector - Adding items in SelectedItems List manually from code. Checkbox not checked

I am having a Long List Multi Selector with Grouping enabled to have jump list feeling for Windows Phone 8 app.
We can select maximum of 8 items in long list multi selector. After selecting the items the items can be seen checked and in code as well I am able to retrieve them via SelectedItems list.
But when I am trying to come back to that page again with same item source to Long List MultiSelector and trying to get Selected Items list pre-selected when Long List Multi Selector is opened the functionality is not happening.
PFB the sample code for the same:-
foreach (var item in SelectedValue)
MultiSelectLongList.SelectedItems.Add(item);
everything else is working correctly in Long List multi selector except for preselecting some items when list is opened.
Is there any other way of doing it. Please help me out.
Thanks
If you are using the Phone toolkit LongListMultiSelector control, this should actually just work.
But you are not showing a lot of code, so we can't see how you are keeping track of the objects and what objects are really set to the ItemsSource of the list.
As you can see in my screenshot, when I just load a LongList with some data and add that same data ( note the word same here because it needs to be the same reference ), the are preselected in the emulator.
If you don't have the correct reference you can itterate the itemssource with linq and get them through some key if needed.
2 ways I pre selected items
this.LongListSelector.SelectedItems.Add(data1);
this.LongListSelector.SelectedItems.Add(this.LongListSelector.ItemsSource[2]);

GWT Watermarked (AKA placeholder) ListBox

I want to have in my gwt application a ListBox with a watermark.
I already created TextBox and a DateBox with a watermark by extending those classes and the property placeholder of the DOM.
However I could not such property for the ListBox. There isn't any, right? (how can I tell? except for trying.)
Assuming that there is no such property I would like to implement such a class by extending ListBox.
I am not sure how such a ListBox would behave.
could you please help me define the behaviour of such ListBox or maybe you know a site that uses one so I could play with it? Or just point me to a code example.
Thanks
I would think it would be a non editable list item as the placeholder. You could have the setPlaceholder set the text on the list item. The placeholder list item would only be visible when the list was empty. You couldn't select the list item, and all methods to access list items would never see it.

UserControl instead of DataTemplates - is there a way for big collections?

There are two ways. May be both are stupid...
I have to display some collections of items.
First one.
I use DataTemplate for ListBoxItem.
Just set itemSource = myCollection;
That's all. Simple scheme.
Second one.
Each item in my collection has property view. It's a UserControl. That define how item renders.
Create DataTemplate with ContentPresenter only.
Binding Content property to a view.
Just set itemSource = myCollection;
That's all. More complex. But works too.
Has second one right to live? My doubt is that I have to create instance of UserControl for every item in my collection?
Is not it too expensive for collection with over than 500 items?
Thanks.
I don't believe there is much difference, with the DataTemplate approach the framework will create an instance of the DataTemplate for each item in the collection. In the second approach an instance of the user control will be created for each item, there may be a few more controls but only a few per item.
One reason the second approach could be preferable is that you can have logic around which content is bound. This could mean different user controls for each item in the list. Caliburn Micro lets you use this approach very naturally.

Windows DataGridView BindingSource Index out of range exception

Scenario:
Basically i have a
System.Windows.Forms.DataGridView
A class that inherits BindingSource and IBindingList
A class that has 2 standard List as private properties
DataGridView dgv = new ...
MyBindingSource bindingSource = new ...
MyList list = new ...
The DataGridView.DataSource property gets set to the BindingSource and the BindingSource.DataSource is set to one of the list's private List
bindingSource.DataSource = list.ListA;
dgv.DataSource = bindingSource;
I am getting a bunch of information streaming in from a database, and i convert the information to objects and add it to the MyList one at a time and in the end should be shown in the DataGridView.
I hope all that made sense, now the problem:
After adding a single object to the list (not the bindingsource) i want the item to be displayed in the DataGridView. But the only way i can currently get that to work is to construct another instance of a bindingSource with the 1 new object appended and set the DataGridView.DataSource to the new bindingSource. This ofcourse is horribly inefficient and the datagridview has to invalidate the whole thing every time, which is dodgy.
Instead i want the List to notifiy the BindingSource which tells the DataGridView a new object has been added so it can do it's thing. I tried this but i kept getting a IndexOutOfrange exception saying 'Item at index -1 does not have a value'.
I had a look at the BindingSource and indeed the position was -1 and the Current property threw that same exception. When i create the new BindingSource every time the position and Current properties are correct.
So what do i have to do in order for those properties to get updated when i add a item to the list? I opened it up with reflector to see where it was being set and looked like "CurrencyManager" had something to do with it. I tried a few things like base.OnDataMemberChanged base.OnListChanged to no avail.
Edit: Forgot to mention i only get the exception when i click on a row in the datagrid view, it adds the items fine. So it's like the DataGridView is out of sync with the BindingSource
When DataSource is changing its content, DataGridView clears its grid and raises SelectionChanged event and other events witch are connected with user interaction. It is iportant to know that when the event is raised there is no data at all to be displayed, so if u got a SelectionChanged event listener and it is accessing rows or columns inside, it will raise out of bound exception when accessing rows. Solution: always check Rows.count against 0 value before use any datagrid resources. Cheers
Have you considered using an ObservableCollection ? Whenever there are items added or removed from the collection it will notifiy the control its bound to that is has been updated. It will also do the same with properties of items in the collection as well ( although you have to raise the event manually )
I'm not to sure what could be causing the exception
If you use a BindingList(generic) instead it will notify of item adds/deletes and individual item changes as well. (http://msdn.microsoft.com/en-us/library/ms132679.aspx)

Resources