WPF: ComboBox only refreshes to binding when never opened - wpf

I have a combobox and set the itemssource to a list of strings:
private List<string> list = new List<string>();
...
ComboBox cbo = new ComboBox();
cbo.ItemsSource = list;
The combobox gets filled successful on startup.
But when the list is changed, the combobox doesn't update its items, BUT only when I expanded the items before, else the combobox gets updated...
Also weird: when I trace the count of items of the combobox, the count is the correct updated number, but the displayed items aren't.
Somebody has an idea, what's going on here?
EDIT: See solution in comments to post of Robert

I guess you need to use ObservableCollection instead of List, because ComboBox needs to be notified whenever the collecion changes.

Change List into ObservableCollection.

Use ObservalbeCollection. It has inbuilt mechanism to notify and change in the number of items.

Related

WPF DataGrid checkbox binding

I'm not sure what I'm missing here, but I'm having trouble getting a checkbox to bind to a list properly. The rest of the properties of the list bind just fine, but the checkbox is having issues. Here's what I have:
In the class that serves as the template for each object in the list I have:
Property Process As New CheckBox
In the MainWindow_Loaded Event I have:
Dim ProcessCol As new DataGridCheckBoxColumn
ProcessCol.Header = "P?"
ProcessCol.IsReadOnly = False
...
InputGrid.ItemsSource = InputData 'Which is a list of my Order Allocation objects which contains the checkbox property
...
Dim ProcessBinding As New Binding("Process")
ProcessBinding.Mode = BindingMode.TwoWay
ProcessCol.Binding = ProcessBinding
...
InputGrid.Columns.Add(ProcessCol)
When I try to populate this collection and look at the items I get checkbox = nothing. I'm not sure what I'm missing here... I know I couldn't be too far off...
Edit: I changed the property to "new CheckBox" and now I get an initialzied checkbox object in the list item as "System.Windows.Controls.CheckBox Content: IsChecked:False which in this case should have been true. So maybe a step closer, but still not there.
I found an answer here: WPF: CheckBox in DataGrid
Technically not an answer to my original question, but it works. Rather than a checkbox property in my class I now have a boolean property. The column is still created as a checkbox column. It works.
If a column can be bound to a chebox property of a list I'd still be interested in hearing that, but for me this solution works.

ListView selection change doesn't remove an old item

I have a strange behavior with my WPF ListView Control.
ListViews ItemSource is Observable collection.the ItemSource is updated periodically.
When I'm selecting one of the item and then selecting other item and no item updated, everything is OK.
But when I'm selecting an item witch is updated while I'm standing on, then selecting other item, now I have two items selected instead of one.
When I'm looking with the debugger, I see the event args of SelectionChanged event. I see that added item is OK but no removed item.
Anyone knows what's the problem?
Thanks!
Edit:
My observable collection:
protected class CustomObservableCollection : ObservableCollection<T>
{
public void Refresh()
{
ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(this));
lcv.Refresh();
}
}
The update method witch called when there is a change in some item:
public void RefreshItem(T domainObject)
{
foreach (T item in obsCollection) {
if (!DomainObjectComparer.Equals(domainObject, item)) continue;
DomainObjectCopier.CopyProperties(domainObject, item);
obsCollection.Refresh();
break;
}
}
It looks like your copier class makes two items in your collection equal (I think, inferring this from the limited amount of code above).
If two items or more are the same (equal) in a ListView, then selecting one will select all of them as an equality comparer is used in the selection logic.
Well, Apparently the problem was with the overridden GetHashCode() method of the ListView item object.
the hash code included all the fields in it's calculation. I remoed all the fields (properties) and now the overridden GetHashCode() is only calculating the hash based on item's ID. it solved the problem.
I also have Equals() method overridden.
If someone knows why it is related I will like to know.

WPF TreeView ItemsSource not keeping values

I am using a wpf treeview and binding the ItemsSource to an IEnumerable of my ViewModel that has an IsChecked Property. I am binding a checkbox to this value with a Mode of TwoWay. I can see when I step through the program that it is setting this value properly on my ViewModel when I check the checkbox.
I then have a Menu Item that "Runs Checked". In this method I have a foreach loop that runs through the ItemsSource as IEnumerable of ViewModel looking for IsChecked = true to queue up the checked items to be run by a separate program. As such:
foreach (AccountViewModel account in tvClientList.ItemsSource as IEnumerable<AccountViewModel>)
{
if (account.IsChecked)
{
context.Queues.InsertOnSubmit(new Queue {Id = account.Id});
}
}
However, account.IsChecked is always false. Why is this?
i would check the following.
does the setter of AccountViewModel.Ischecked is called when clicking the treeview checkbox. if no - then thats the problem if yes you should look at your collection bindings.
debug your foreach and check wether the treeviewitemssource is the collection you expect.
maybe you can post your bindings, if that all no help.
ps: dont access your View controls directly if you use viewmodels/mvvm.
Just for anyone else encountering this issue. ItemsSource returns a new set of data, Items, however contains the current set of data.

Adding an Item to ItemsSource from a control

A control has an ItemsSource Property of type IEnumerable. If I try to add items to Items collection when ItemsSource is set I get an error "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
A method for Removing Items is present here:
WPF - Best way to remove an item from the ItemsSource
However I cannot find a method to (based on the same interface) to add a new Item. The AddNew method does not take any arguments. From the sample at : http://msdn.microsoft.com/en-us/library/system.componentmodel.ieditablecollectionview.canaddnew.aspx I felt this to be the correct code:
IEditableCollectionView items = paneToDropInto.Items;
if (items.CanAddNew)
{
object newitem = items.AddNew();
newitem = contentToTransfer;
items.CommitNew();
}
However it does not work. It does add the new item. But it is a blank Item. Note contentToTransfer.
Figured it out. As pointed out by Tom and Djerry +1 both (thanks). I am just re referencing the new item which will not cause the original new item generated by AddNew to be saved (very stupid of me).
However there is another interface I can use (and did use):
IEditableCollectionViewAddNewItem items = paneToDropInto.Items;
if (items.CanAddNewItem)
{
object newitem = items.AddNewItem(contentToTransfer);
}

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