-1

我有这样的课:

public class ResourceList : ObservableCollection<Resource>
{
    public ResourceList() : base()
    {
    }
}

public class Resource
{
    public Resource()
    {
        Name = "";
        EMail = "";
        Date = "";
        Time = "";
        SWList = new ObservableCollection<string>();
    }
    public string Name { get; set; }
    public string EMail { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
    public ObservableCollection<string> SWList { get; set; }
}

public class Licenses : ObservableCollection<Licenses>
{
    public Licenses()
    {
    }
    public string Name { get; set; }
    public string License { get; set; }
}

以及以下两个命令,它们看起来几乎相同:

internal class ReadResourceFilesCommand : ICommand
{
    public ReadResourceFilesCommand(ResourceViewModel viewModel)
    {
        _viewModel = viewModel;
    }

    private ResourceViewModel _viewModel;

    #region ICommand Members

    event EventHandler ICommand.CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    bool ICommand.CanExecute(object parameter)
    {
        return _viewModel.CanUpdate;
    }

    void ICommand.Execute(object parameter)
    {
        _viewModel.ReadResourceFile(parameter.ToString());
    }
    #endregion
}



internal class ReadInventoryFilesCommand : ICommand
{
    public ReadInventoryFilesCommand(ResourceViewModel viewModel)
    {
        _viewModel = viewModel;
    }

    private ResourceViewModel _viewModel;

    #region ICommand Members

    event EventHandler ICommand.CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    bool ICommand.CanExecute(object parameter)
    {
        return _viewModel.CanUpdate;
    }

    void ICommand.Execute(object parameter)
    {
        _viewModel.ReadInventroyFiles(parameter.ToString());
    }
    #endregion
}

CanUpdate和我的 viewModelResourceList看起来像这样:

public bool CanUpdate
{
    get
    {
        if (ResourceList == null)
        {
            return false;
        }
        return true;
    }
}

public ResourceList ResourceList
{
    get
    {
        return _ResourceList;
    }
    set
    {
        if (value == _ResourceList)
            return;

        _ResourceList = value;

        // INotifyPropertyChanged Method for updating the binding
        onPropertyChanged("ResourceList");
    }
}

现在实际问题:当第一个命令被执行时,不管是哪个命令,GUI 中的listView填充,一切看起来都很好。但是当执行另一个命令时(实际上更新了同一个列表中的元素,所有元素的时间日期属性都会更新)listView不会更新它的元素。

我知道我没有更改整个列表,我正在更新其中的一些元素,这可能就是它没有被更新的原因。但我真的不知道该怎么办?请解释我应该如何修改代码!

4

1 回答 1

2

Resource 类不实现 INotifyPropertyChanged。

这个接口导致视图被通知资源实例的变化。就像 ObservableCollection 通知 View 对集合的更改(添加、删除、移动项目)

public class Resource : INotifyPropertyChanged
{
    public Resource()
    {
        Name = "";
        EMail = "";
        Date = "";
        Time = "";
        SWList = new ObservableCollection<string>();
    }

    // TODO: do this for all properties:

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



    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName);
        }
    }
}
于 2013-10-21T06:30:24.143 回答