- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我注意到在将对象添加到 ViewModel 属性时 RaisePropertyChanged 有一个奇怪的行为。
private List<string> _items;
public List<string> Items
{
get
{
if(_items == null){ _items = new List<string>(); }
return _itmes;
}
set
{
_items = value;
RaisePropertyChanged("Items");
}
}
每当我通过属性向集合中添加对象时
Items.Add("new string");
RaisePropertyChanged 永远不会被调用。
让 RaisePropertyChanged 以我希望的方式运行的最佳方法是什么?
最佳答案
当您更改集合时将调用您的 setter,如果集合的内容已更改则不会。您需要的是一个通知您有关更改的集合。看 ObservableCollection<T>
-类(class)。注册到其CollectionChanged通知更改的事件。
带有 setter 的属性
以下示例向您展示了如何使用包含可观察集合的可设置属性。该示例的复杂性是因为可以从实异常(exception)部设置集合。如果您不需要此功能,解决方案将变得更加简单。
private ObservableCollection<string> _items;
public ObservableCollection<string> Items {
get {
if (_items == null) {
// Create a new collection and subscribe to the event
Items=new ObservableCollection<string>();
}
return _items;
}
set {
if(value !=_items){
if (null != _items) {
// unsubscribe for the old collection
_items.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged);
}
_items = value;
if (null != _items) {
// subscribe for the new collection
_items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged);
}
// Here you will be informed, when the collection itselfs has been changed
RaisePropertyChanged("Items");
}
}
}
void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
// Here you will be informed, if the content of the collection has been changed.
}
没有 setter 的属性
如果您不需要设置集合,请注册到 CollectionChanged
在创建集合时。但是,您必须删除您的属性(property)的二传手。否则,如果集合已更改,则不会通知更改:
private ObservableCollection<string> _items;
public ObservableCollection<string> Items {
get {
if (_items == null) {
// Create a new collection and subscribe to the event
_items=new ObservableCollection<string>();
_items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler
}
return _items;
}
}
void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
// Here you will be informed, if the content of the collection has been changed.
}
附加信息
看INotifyCollectionChanged 有关集合更改的更多信息。上面的例子你也可以使用更通用的 IEnumerable<string>
和 INotifyCollectionChanged
.
关于c# - ViewModel 属性的异常行为,未执行 RaisePropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4879947/
我不确定为什么要使用 RaisePropertyChanging , 当通知 View fx: private LoggingLvl _myLoggingLvl; public Loggin
在这里,我向我的 View 模型添加了一个模型: public dal.UserAccount User { get { return _user; } set {
我已经将 MVVM 与我的 WPF 应用程序一起使用了一段时间,但是我试图让一个按钮保持禁用状态,直到它可以启用为止。没问题,我将在 ViewModel 中创建一个 bool 并将其绑定(bind)到
我有一个按钮,其内容与 ViewModel 的名为 Test 的属性绑定(bind)。 单击按钮时,我正在更改其内容 private void button1_Click(object sender
我有一个模型用户: public class User { public string Name { get; set; } public int Level { get; set;
为了在通用应用程序中获取 MvvmCross PropertyChanged 事件,我必须设置 ShouldAlwaysRaiseInpcOnUserInterfaceThread(false)。你能
我在模型类的 IDataErrorInfo 的实现部分中编写了此代码,以便在清除错误后清除错误。 我不明白这里用空字符串引发了哪个属性刷新? if (raiseNotification)
我有以下用于单选按钮绑定(bind)的类 public class RadioButtonSwitch : ViewModelBase { IDictionary _optio
我无法在 View Model 的 SET 属性中定位断点,因此默认值未更改。 (Get - 没问题,它用有效的默认值初始化我的文本框。) 我有一个model.cs定义公共(public)字符串字
如果我想通过 RaisePropertyChanged 更新我的 UI,我会遇到一个奇怪的行为。我使用 this post 的第二个解决方案(由 Johnathan1 提供) :我实现了 RadioB
我正在使用 mvvm-light,我注意到有关 RaisePropertyChanged 的这种奇怪行为。 xaml: 可观察类: public class A : ObservableObj
根据 MSDN doc on PropertyChanged ,如果将空字符串传递给该方法,则会刷新所有属性。使用MvvmLight RaisePropertyChanged 时有这样的事情吗?传递
我目前正在学习 MVVM,我不明白 RaisePropertyChanged 有多重要 例如这个函数: public City SelectedCity { get {
我在 Windows Phone Silverlight 应用程序中使用 MVVM Light。 我真的不明白 RaisePropertyChanged 应该如何工作;让我解释一下,用这样的代码 pr
我正在寻找一种解决方案,以在其中一个内部属性发生变化时触发整个属性的 PropertyChanged。所以这是模型: public class MainStatus : ObservableObjec
由于某种原因,我无法让 UI 响应 RaisePropertyChanged。卡住了。 CurrentClient 是不向 UI 触发的部分。真的很奇怪。 希望有人可以帮助我。谢谢斯科特 public
我有 public override bool RelatedProperty { get { return this.SomeProperty > 0; }
NotificationObject 中此方法有一个重载:- protected void RaisePropertyChanged(Expression> propertyExpression);
当我的 ViewModel 更改时,我无法让我的 UI 更新两个列表框以正确更新。 首先,页面背后的基本逻辑: Movie 是一个带有标题的对象,以及各种MovieDetails。一些 MovieDe
我有一个类似于此的属性: private ISomething currentSomething; public ISomething CurrentSomething {
我是一名优秀的程序员,十分优秀!