WPF ListBox insert - wpf

I have a WPF Listbox defined in a DataTemplate that has its ItemsSource bound to an ObservableCollection. When new items are added to the collection, I want the ListBox to work in "insert mode", always adding to the front of the list, not the default append mode.
How do I make the ListBox use the insert mode when not adding to it directly in code?

Use the Insert method of the ObservableCollection and pass the index 0 - that will add it to the beginning of the collection.

Related

WPF window inserting an array into DataGrid

WPF window in C# doesn't have option of Inserting something into DataGrid immediately, like as WinForms Form
DataGridView.Rows.Add(whatever)
What is alternative for this code 👆?
So how can I insert an array into DataGridView in WPF window?
You can actually add objects directly to the Items property of the DataGrid:
dataGrid.Items.Add(whatever);
But if you want to be able to edit the items, you should set or bind the ItemsSource property to an IList:
dataGrid.ItemsSource = new List<object> { whatever };
You need to bind the DataGrid to an ObservableCollection. If you insert into the ObservableCollection, it'll appear on the UI

How do I loop through a set of items in a databound Silverlight control such as the ListBox?

I have a silverlight ListBox that has it's ItemsSource set. From the C# code behind I want to loop through each ListBox item and change the value of the text and access controls that are in the ListBox ItemTemplate. How would I do that?
ListBox controls in silverlight are bound to an ienumerable type, so that if any value in the ListBox changes, the underlying data is changed and vice versa depending on what type of binding you require. To effectively iterate the items you'll want to iterate through the enumerable object you've bound to.
You can get to the collection by accessing the ListBox.ItemsSource property and change the text of appropriate items, perform LINQqueries etc. If you have bound the controls correctly, saving the collection should update the list.
Hope this helps!

WPF TabControl add extra tabs to a bound control

I have a Tab Control with a ItemsSource Binding.
...
I want to add a predefined tab to the front called All that has an aggregate of all the other tabs, and I would also like to add a button at the end called Add so I can add a new tab.
Is there an easy way of doing this ?
Thanks,
Raul
The easiest way is to go with MVVM (the example in the url acutally contains TabControl bound to a ViewModel). Your ViewModel that you bind your TabPages against could expose an observablecollection of items where the first item is always a ViewModel instance that holds you aggregate data. All follwing items are the ViewModel instances for the rest of the tabpages. Your ViewModel would also expose a ICommand AddTabPage wich adds a new item to the obeservablecollection. The TabPage will pick up this change automatically. You'd have a button whose Command property is bound to this command.

Linq to SQL Compact - Update binding

When I set the ItemsSource of a ListBox to the contents of a table, like this:
this.listBox.ItemsSource = db.Table;
The items are not updated automatically in the ListBox. How can I manage to update the ListBox automatically when items are added, removed or changed? And can I also receive an event when the collection has changed?
Take a look at ObservableCollection. I'm using it to update/add/delete a listview. When I change the ObservableCollection, the listview gets notified.

Silverlight listbox won't update with new itemsource

I have a listbox which is bound to a generic list, whenever I removed an item from the generic list and rebind it to the listbox it still shows the deleted items. Here is the code :
InventoryList.Remove(currInv);
lstSubMenu.ItemsSource = InventoryList;
lstSubMenu.DisplayMemberPath = "InventoryItemName";
I checked the generic list and the item is being removed and there doesn't seem to be any errors in the output window.
Set the ItemsSource = null before you set it to be InventoryList.
However, it is generally better practice to set the ItemsSource property once and never again. You can do this by using an ObservableCollection. Once you do this you can add/remove to your heart's content and not have to worry about the binding target not getting updated.

Resources