UI与ObservableCollection不同 [英] UI isn't resembling ObservableCollection

查看:140
本文介绍了UI与ObservableCollection不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ListView更改为我在ObvservableCollection中修改的任何值.

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

我当前正在使用它,并将Collection绑定到我的ListView

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?

xaml的第一行是:

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

任务类别:

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

推荐答案

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

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.

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

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

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

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.

这篇关于UI与ObservableCollection不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆