繁体   English   中英

当集合中的项目发生更改时,Linq 数据绑定更新

[英]Linq databinding update when item in collection has changed

当绑定到我的视图的项目发生更改时,我希望更新数据库。 我注意到 PropertyChanged 事件在 linq class 中触发,但是我如何告诉我的视图模型发生了一些变化? 我使用 Linq class 作为我的 model 所以我不必重新创建它,这是不好的做法吗? I know that I could create a new property in my Linq class true or false and use that property from my viewmodel but that wouldn't be too efficent since I would have to redo that each time I needed to update the class from SQL.

  • View 和 Model 之间的分离是 ViewModel 的关键职责之一。 否则 Model 将被视图特定属性和数据格式污染。
  • ViewModel 应该将 Model 包装为属性并将它们公开给 View,并且当 View 修改包装的 ViewModel 属性时,它可以依次调用 model 特定方法。

因此,不要将您的 linq 类直接绑定到您的视图,而是通过 ViewModel 中的属性公开它们。 就个人收集项目通知而言,这里是您可以使用的东西

只是为了让其他人知道我为这种情况做了什么。 我确实尝试了 anivas 发布的链接并且它有效,但并不像我为我的解决方案提出的那样简单。 我将所选项目绑定到我的模型视图上的一个属性,因为它是唯一可以由用户更改的。 在我的属性的设置器上,我为 notifyProperty 更改了一个处理程序。 请参阅下面的代码 private CustAccountLocation _selectedStore;

    public CustAccountLocation SelectedStore
    {
        get { return _selectedStore; }
        set {

            _selectedStore = value;
            SelectedStore.PropertyChanged += new PropertyChangedEventHandler(SelectedStore_PropertyChanged);
            NotifyPropertyChanged("SelectedStore");
        }
    }

    void SelectedStore_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        StoreNeedsSave = true;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM