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

发布于 2024-09-16 06:33:07 字数 1884 浏览 3 评论 0原文

我的上下文:Entity Framework 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. 当用户在名字、姓氏文本框中键入一些内容时,应启用该按钮。

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

如何解决这个问题?

my context: Entity Framework 4.0 + WCF Ria Data Service + Silverlight 4.
Suppose I have entity People from EF, then I created PeopleVM like:

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
        {
    //.....
        }
   }

in UI xaml, I set binding as:

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

Then I set button in UI as:

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

So what I want is:

  1. initially, button should be disabled because there is no data changes.

  2. when user type some in FirstName, LastName textbox, the button should be enabled.

But with above code, even I change firstname, lastname, the button still in disable status.

How to resolve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-09-23 06:33:07

问题是当域上下文 HasChanges 属性更新时,视图模型的 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}"/>

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

The problem is that your viewmodel's HasChanges property is not updated when your domain context HasChanges property is updated. I typically just expose the Domain Context as a property on the view model and bind the buttons enabled state directly to its HasChanges property.

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
        {
    //.....
        }
   }

Here is the XAML for the UI to bind to the context's HasChanges:

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

You must make sure that the People object is pulled out of the DomainContext in order for updates to affect the HasChanges property of that DomainContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文