【问题标题】:How do entity self-tracking with changes in VM and UI?实体如何根据 VM 和 UI 的变化进行自我跟踪?
【发布时间】:2024-04-14 16:20:01
【问题描述】:

我的上下文:实体框架 4.0 + WCF Ria 数据服务 + Silverlight 4。 假设我有来自 EF 的实体 People,然后我创建了 PeopleVM,例如:

public class PeopleViewModel : ViewModelBase
    {
        public PeopleViewModel(){
       //....
           this._hasChanges = MyDomainContext.HasChanges;
        }

        private People _People;
        public People People
        {
            get { return _People; }
            set
            {
                if (value != this._People)
                {
                    value = this._People;
                    this.RaisePropertyChanged("People");
                }
            }
        }

        private bool _hasChanges;
        public bool HasChanges
        {
            get { return this._hasChanges; }
            set
            {
                if (this._hasChanges != value)
                {
                    this._hasChanges = value;
                    this.RaisePropertyChanged("HasChanges");
                }
            }
        }

        //........

        private RelayCommand _saveCommand = null;
        public RelayCommand SaveCommand
        {
    //.....
        }

        private RelayCommand _cancelCommand = null;
        public RelayCommand CancelCommand
        {
    //.....
        }
   }

在 UI xaml 中,我将绑定设置为:

<TextBox Text="{Binding People.FirstName, Mode=TwoWay}" />
<TextBox Text="{Binding People.LasttName, Mode=TwoWay}" />

然后我将 UI 中的按钮设置为:

<Button Content="Save" Command="{Binding SaveCommand}"  IsEnabled="{Binding HasChanges}"/>
<Button Content="Undo" Command="{Binding CancelCommand}" IsEnabled="{Binding HasChanges}"/>

所以我想要的是:

  1. 最初,应该禁用按钮,因为没有数据更改。

  2. 当用户在 FirstName、LastName 文本框中输入一些内容时,该按钮应启用。

但是使用上面的代码,即使我改变了名字,姓氏,按钮仍然处于禁用状态。

如何解决这个问题?

【问题讨论】:

  • 您确定在实例化PeopleViewModelMyDomainContext.HasChanges 的值不为空吗?尝试在那里放置断点。

标签: mvvm silverlight-4.0 entity-framework-4 wcf-ria-services


【解决方案1】:

问题是当您的域上下文 HasChanges 属性更新时,您的 viewmodel 的 HasChanges 属性没有更新。我通常只是将域上下文公开为视图模型上的一个属性,并将按钮启用状态直接绑定到它的 HasChanges 属性。

public class PeopleViewModel : ViewModelBase
    {
        public PeopleViewModel(DomainContext context){
            this.Context = context;
        }

        public DomainContext Context
        { get; private set; }


        private People _People;
        public People People
        {
            get { return _People; }
            set
            {
                if (value != this._People)
                {
                    value = this._People;
                    this.RaisePropertyChanged("People");
                }
            }
        }

        //........

        private RelayCommand _saveCommand = null;
        public RelayCommand SaveCommand
        {
    //.....
        }

        private RelayCommand _cancelCommand = null;
        public RelayCommand CancelCommand
        {
    //.....
        }
   }

这里是 UI 绑定到上下文的 HasChanges 的 XAML:

<Button Content="Save" Command="{Binding SaveCommand}"  IsEnabled="{Binding Context.HasChanges}"/>
<Button Content="Undo" Command="{Binding CancelCommand}" IsEnabled="{Binding Context.HasChanges}"/>

您必须确保将 People 对象从 DomainContext 中拉出,以便更新影响该 DomainContext 的 HasChanges 属性。

【讨论】:

    最近更新 更多