I have a listbox bound to an observable collection of products. Each products has an IsFavorite bool property bind to an Image using a convertor.
I fill in the observable collection and all the product have IsFavorite=false. Then i load favorites from Isolated Storeage and in an foreach update the IsFavorite property (calling NotifyPropertyChanged) for each product in the observable collection bound to listbox. The Image does not change for the products in view. If I scoll away and then return to a changed item, the icon changes.
What is the problem? How can I force binding to refresh immediately not after scolling?
I know you said you are raising the PropertyChanged event but I am afraid your class "Product" is not implementing INotifiedPropertyChanged and incidently not raising the propertyChanged event when the bool IsFavorite changes.
Try implementing INotifiedPropertyChanged in your product class.(and on the Product.IsFavorite property)
Related
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");
I am dynamically loading around 1000+ items which are translated to buttons in xaml.
The view model has an IsEnabled property that is binded to the IsEnabled property of the button in the View.
My question is, won't this affect performance? I only need the property on some of the items but all of them use the same viewmodel and it is quite hard to introduce a new view model just for that purpose.
I am using Windows Store 8.1.
Thank You!
1k bool bindings should not do any harm. I've seen applications with 5-10k bindings beeing populated at once, they can freeze a little bit if done in sync. Your item/list in viewmodel loading should be done async to avoid UI freeze if the collection of those items is bound instantly with all the items already there, that is why observable collection is a fundamental thing to bind to.
I have a view with a list box, bound to am obvervable collection of DisplayItems, which has 'Label', 'DisplayValue' and 'IconUri' properties.
I have a View Model which exposes this observable collection. The List Box is correctly populated first time around.
I then have a button which takes action on the selected item. I need to indicate that action has been taken by changing the image.
I am changing the IconUri of the selected item, and can see the new value present when debugging, but the image doesn't change. I can also change the 'Label' and 'DisplayValue' properties and see the new values correctly there when debugging, but the list doesn't change.
My ViewModel implements INotifyPropertyChanged. My DisplayItem class implements INotifyPropertyChanged. I'm calling RaisePropertyChanged I'm sure in too many places rather than too few.
None of the changes are ever reflected on screen.
I'm using a DataItemTemplate for the generated rows. If I could access the image of the selected row I could change it manually, but I can't even do that.
Any help greatly appreciated. I could actually do with a example of a list box displaying items from a bound observable collection, where one property of the selected item is changed and that change is reflected in the list box.
Thanks in advance
A
You didn't implement INotifyPropertyChanged correctly, or you're using it wrong.
I had a quick question on working with listboxes in WP7 using MVVM Light. Basically before I was using MVVM all I had to do was set the SelectedIndex to -1 inside of the OnNavigatedTo event when the page was navigated to. Then inside of the SelectionChanged event I would check if the SelectedIndex was equal to -1 and if so I would ignore it. The reason I did this was just in case the user wanted to select the same item again when they came back to the page.
Now with MVVM (MVVM Light) I'm binding the event to a command, which takes care of the first part. But now I'm stuck because I don't know how to set the SelectedIndex to -1 from the ViewModel which prevents the user from selecting the same item again. Any ideas?
An even better solution is to not use the selection event to trigger a navigation. Use the tap event of the indivdual item instead.
This also avoids issues of accidental navigation when scrolling.
Try creating a SelectedItem property on your viewmodel, then do a two-way between the SelectedItem viewmodel property and the SelectedItem property on your ListBox. Then you can update it with whatever value you want when your Command fires and the result will propagate back to your ListBox.
I am using a listbox in a data template - and from an earlier post I cannot reference the listbox directly in the code behind.
As a result I am capturing the last selected object in the selectionchanged event for the listbox and using this when I want to navigate.
I now need to also clear the selected object in the listbox -can I do this in the selectionchanged event (after storing it away).
Alternatively I could use the MouseLeftButtonDown event on the listbox (which I understand is the equivalent of a 'click') but can I get the selected object in the listbox in this event.
thanks
In the selection changed event set <ListboxName>.SelectedIndex = -1;
Also, do not use the MouseLeftButtonDown event. This will fire whenever the user touches the ListBox, even if they're just trying to scroll up / down and not actually selecting an item.
If you can't change the SelectedIndex in code behind then, instead of detecting the SelectionChanged event you could detect a Tap event on the ListBoxItem.