WPF MVVM更新了元素而没有更新GUI

时间:2013-10-21 06:24:57

标签: c# .net wpf mvvm

我是MVVM的新手,所以请光临我,如果问题不够清楚,请告诉我,我会添加更多信息。

我有一个这样的课程:

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
}

我的viewModel CanUpdateResourceList如下所示:

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 正在填充,一切正常。但是当执行另一个命令时(实际更新了同一列表中的元素,所有元素的 Time Date 属性将被更新) listView < / strong>不会更新其元素。

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

1 个答案:

答案 0 :(得分:2)

Resource类未实现INotifyPropertyChanged。

此接口使View通知Resource实例的更改。就像ObservableCollection通知Vi​​ew对集合的更改(添加,删除,移动项目)

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