gpt4 book ai didi

wpf - 如何在 MVVM 中实现 INotifyCollectionChanged 以重置、添加、删除、移动、替换 NotifyCollectionChangedAction

转载 作者:行者123 更新时间:2023-12-03 10:13:32 30 4
gpt4 key购买 nike

我的 ViewModelBase 类是:

public abstract class ViewModelBase:INotifyPropertyChanged, IDisposable, INotifyCollectionChanged
{
#region Constructor

protected ViewModelBase()
{
}

#endregion // Constructor
#region DisplayName

/// <summary>
/// Returns the user-friendly name of this object.
/// Child classes can set this property to a new value,
/// or override it to determine the value on-demand.
/// </summary>
public virtual string DisplayName { get; protected set; }

#endregion // DisplayName


#region Debugging Aides

/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;

if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}

/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

#endregion // Debugging Aides
#region INotifyPropertyChanged Members
/// <summary>
/// raised when property of this object has some new value
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

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

#endregion

#region IDisposable Members

public void Dispose()
{
this.OnDispose();
}

/// <summary>
/// child classes can override this method to perform cleanup logic,like removing eventhandlers and disposing objects
/// Anindya
/// </summary>
protected virtual void OnDispose()
{
//no implementation has been done here
//intentionhally I have done so
//so that this method will be only used for the overriding of this method
//by default nothing I have kept in this method
}

#endregion

#region INotifyCollectionChanged Members
/// <summary>
/// Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;

protected virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent)
{
//NotifyCollectionChangedEventHandler handler = this.CollectionChanged;
//if (handler != null)
//{
// var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction
//}
}

#endregion

}

我的 WorkSpaceViewModel 继承自 ViewModelBase 如下:

public abstract class WorkspaceViewModel:ViewModelBase
{
#region Fields

RelayCommand _closeCommand;


#endregion // Fields

#region Constructor

protected WorkspaceViewModel()
{
}

#endregion // Constructor

#region CloseCommand

/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public ICommand CloseCommand
{
get
{
if (_closeCommand == null)
_closeCommand = new RelayCommand(param => this.OnRequestClose());

return _closeCommand;
}
}

private void CanDoSomeImportantMethod()
{
}
#endregion // CloseCommand

#region RequestClose [event]

/// <summary>
/// Raised when this workspace should be removed from the UI.
/// </summary>
public event EventHandler RequestClose;

void OnRequestClose()
{
EventHandler handler = this.RequestClose;
if (handler != null)
handler(this, EventArgs.Empty);
}

#endregion // RequestClose [event]
}

我的 ViewModel 继承自 WorkSpaceViewModel,如下所示:

public class MainWindowViewModel:WorkspaceViewModel,INotifyCollectionChanged
{
//
RelayCommand _loadCommand;
MatchBLL matchBLL = new MatchBLL();
EfesBetServiceReference.EfesBetClient proxy = new EfesBetClient();
public MainWindowViewModel()
{
_matchObsCollection = new ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC>();
Load();

_matchObsCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(_matchObsCollection_CollectionChanged);
}

/// <summary>
/// This will get called when the collection is changed(for reference see http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _matchObsCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{

}

protected override void OnPropertyChanged(string propertyName)
{
base.OnPropertyChanged(propertyName);
}
public ICommand LoadCommand
{
get
{
if (_loadCommand == null)
{
_loadCommand = new RelayCommand(
param => this.Load(),
param => this.CanLoad
);
}
return _loadCommand;
}
}

List<EfesBet.DataContract.GetMatchDetailsDC> matchList;
ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC> _matchObsCollection;

public ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC> MatchObsCollection
{
get { return _matchObsCollection; }
set
{
_matchObsCollection = value;
OnPropertyChanged("MatchObsCollection");
}
}

public void Load()
{
matchList = new List<GetMatchDetailsDC>();
matchList = proxy.GetMatch().ToList();
foreach (EfesBet.DataContract.GetMatchDetailsDC match in matchList)
{
_matchObsCollection.Add(match);
}
//ajebaje code
PopulateSahibiKonuk();
}

bool CanLoad
{
get { return true; }
}


#region INotifyCollectionChanged Members

public event NotifyCollectionChangedEventHandler CollectionChanged;

#endregion

现在我在 UI 中有一个 DataGrid,我想要我的 ObservableCollection 的 OnCollectionChanged。 NotifyCollectionChangedAction,如添加、移动、删除、替换、重置我的 View 模型应该如何触发。但是我不知道如何在我的基类或我的 View 模型中实现或我必须做什么。请提供一些有用的代码或 url 或相关建议。

在此先感谢您。

最佳答案

通常, View 模型不会实现INotifyCollectionChanged接口(interface)...这是供集合类实现的。我有一个庞大的 WPF 项目,我一次都不需要实现该接口(interface)。

我通常使用扩展 ObservableCollection<T> 的自定义集合类这些集合已经实现了 INotifyCollectionChanged界面。因此,当我需要显示集合时,我只需在我的 View 模型类中为这些集合添加属性。如果我需要监视集合中的更改,我会为 CollectionChanged 添加一个处理程序事件。

这不是可以构建到基本 View 模型中的东西,除非您确定每个 View 模型都将具有特定类型的集合。即便如此,当您在一个 View 模型中需要多个集合时,您会怎么做?您必须添加一个集合属性和额外的处理程序,那么为什么不在需要时就这样做呢?

关于wpf - 如何在 MVVM 中实现 INotifyCollectionChanged 以重置、添加、删除、移动、替换 NotifyCollectionChangedAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202333/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com