Modify Listbox item

Jul 7, 2011 at 1:14 PM

Hi at all,

I've a ListBox bound to an IObservableCollection in the view model, when i edit an item of that collection and i call NotifyOfPropertyChange but the item in the listbox does not update
This is the method i use


public void Edit()
	((Row1)List[0]).Text = "Edited row1";
	NotifyOfPropertyChange(() => List);

List is the IObservableCollection boud to the Listbox
how can i do?


Jul 7, 2011 at 8:51 PM

Sounds strange: calling NotifyOfPropertyChange on the property exposing the collection should force a *FULL* ListBox refresh. 
Could you please post the List property and the ListBox binding snippets?

By the way, I would like to point out that this approach could quickly lead to sluggish UI with an high number of items. 
Unless you actually intended to refresh the whole list, you should implement INotifyPropertyChanged on the Row1 class.  
That will cause the UI to just update the control bound to Text property on the row #0 as soon as the Text property is changed.


Jul 8, 2011 at 1:32 PM

This is mi ShellViewModel class

namespace Notifing
	using System.ComponentModel.Composition;
	using Framework;
	using System.Collections.Generic;

	public class ShellViewModel : Screen, IShell
		public IList<string> Lista { get; set; }

		public ShellViewModel()
			Lista = new List<string>();

		public void Add()
			NotifyOfPropertyChange(() => Lista);

and this is my the xaml of the ShellView

<Window x:Class="Notifing.ShellView"
				Width="400" Height="400">

	<Grid Background="White">
		<ListBox ItemsSource="{Binding Lista}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,40"/>
		<Button x:Name="Add" Content="Add" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="75" Height="23" Margin="10"/>

this is a very simple example but it doesn't work

Jul 8, 2011 at 1:53 PM

First, you should use an ObersvableCollection<string> (or Caliburn.Micro's BindableCollection<string>) because what you are doing is very inefficient...if it were to work. The reason it doesn't work is because WPF is smart and even though you raised the change notification, it detects that you are still referencing the same instance, so it thinks it doesn't need to refresh the data. Just to reiterate, you should use some sort of ObersvableCollection if you want to track changes in list contents. You should only do what you are showing if you need to replace an entire list with a completely different list, which will only work if the new list fails the equality check.

Jul 8, 2011 at 2:42 PM

Ouch! I forgot the gotcha of WPF equality check; SL behaves diffently, perhaps?

@Gigitsu: The first snippet is about changes in a single item. Is that issue solved?