实体如何通过VM和UI的变化进行自我跟踪?

时间:2010-08-26 13:48:59

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

我的上下文:实体框架4.0 + WCF Ria数据服务+ Silverlight 4。 假设我有来自EF的实体人员,那么我创建了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文本框中键入一些内容时,应该启用该按钮。

  3. 但是使用上面的代码,即使我更改了firstname,lastname,该按钮仍处于禁用状态。

    如何解决此问题?

1 个答案:

答案 0 :(得分:1)

问题是,当您的域上下文HasChanges属性更新时,您的viewmodel的HasChanges属性不会更新。我通常只将Domain Context公开为视图模型上的属性,并将按钮启用状态直接绑定到其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属性。