1

我试图让 ListView 更改为我在 ObvservableCollection 中修改的任何值。

我目前正在使用它并将集合绑定到我的 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");
            }
        }

问题是如果我要使用以下代码:

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

该值仍将与以前的值相同,即使它应该已更改。

谁能指出我可能遇到的问题?

xaml 的第一行是:

<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">

任务类:

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));
        }
    }
4

1 回答 1

7

ObservableCollection 将处理从集合中添加或删除项目的情况,但不会处理更改集合中项目的属性的情况。根据 MSDN: 表示动态数据集合,在添加、删除项目或刷新整个列表时提供通知。

您需要确保您的 Task 类实现 INotifyPropertyChanged 接口,并在其 TaskNumber 属性更改时触发 PropertyChanged 事件:

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

您的 Task 类实现了 INotifyPropertyChanged,但当其属性的值发生更改时,它实际上从未触发 PropertyChanged 事件。当您的每个属性更改时,您需要手动调用 OnPropertyChanged - 它不会自动发生。

于 2012-08-09T22:04:21.603 回答