Sandeep Bansal
Sandeep Bansal

Reputation: 6394

UI isn't resembling ObservableCollection

I'm trying to get a ListView to change to whatever values I modify in my ObvservableCollection.

I'm currently using this and binding the Collection to my ListView

private ObservableCollection<Task> _tasks = TaskInit.TaskCollection;
        /// <summary>
        /// Observable Collection to hold all tasks
        /// </summary>
        /// 

        public ObservableCollection<Task> TasksCollection
        {
            get { return _tasks; }
            set
            {
                _tasks = value;
                NotifyPropertyChanged("TasksCollection");
            }
        }

The problem is if I were to use the following code:

TaskInit.TasksCollection[2].TaskNumber = "hi";

The value would still be the same as the previous even though it should've changed.

Can anyone point out the problem I may have made?

The first line of the xaml is:

<ListView SelectionMode="Single" ScrollViewer.CanContentScroll="True" x:Name="listTasks" Margin="0,0,0,35" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding TasksCollection, Mode=TwoWay}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="0" FontFamily="Myriad Pro" Foreground="{x:Null}" IsSynchronizedWithCurrentItem="True">

Task Class:

public class Task : INotifyPropertyChanged
    {
        /// <summary>
        /// 
        /// </summary>
        public string Module { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FirstValue { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string SecondValue { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ThirdValue { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Minutes { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string TaskNumber { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Story1 { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Story2 { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Story3 { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string NetworkAdapter { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Upvotes: 1

Views: 94

Answers (1)

Marty
Marty

Reputation: 7522

An ObservableCollection will handle the case where items are added or removed from the collection, but it won't handle the case where properties of items in the collection are changed. As per MSDN: Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

You'll need to make sure your Task class implements the INotifyPropertyChanged interface, and fires PropertyChanged events when its TaskNumber property changes:

 private string _taskNumber;
 public string TaskNumber
 {
     get { return _taskNumber; }
     set
     {
         if(_taskNumber!= value)
         {
            _taskNumber= value;
            OnPropertyChanged("TaskNumber");
         }
     }
 }

Your Task class implements INotifyPropertyChanged, but it never actually fires PropertyChanged events when the values of its properties change. You need to manually call OnPropertyChanged when each of your properties change - it doesn't happen automatically.

Upvotes: 7

Related Questions