Databinding of IsDirty

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 11-Jul-2012 11:40:16   

Using LLBLGen 3.5, Adapter General2010, SQL2008R2, .NET 4.0, VS2010, WPF

Hello,

I'm using LLBLGen generated code all the way up to my viewmodel-layer so I can use all the built-in plumbing of NotifyPropertyChanged (works wonderfully)

Now one of the requiremens is to show an asterisk in the tab-labels when an entity "IsDirty" (or one of it's subcollections ContainsDirtyContents). I would like to do this via a Binding in WPF, Something like :


        <TextBlock Text="*" Visibility="{Binding Entity.IsDirty,Converter={StaticResource booleanToVisibility}}"/>

I was surprised to find out that the IsDirty property on an entity is not out-of-the-box notified of changes. But of course this could quickly be solved by adding the following piece in CommonEntityBase:


        protected override void OnInitialized()
        {
            this.EntityContentsChanged += (sender, args) => OnPropertyChanged("IsDirty");
            base.OnInitialized();
        }

But this doesn't solve the problem of the notification of "dirtyness" when the subcollections contain dirty items.

I was thinking of some kind of property like this (on CommonEntityBase) :


        public bool HasDirtyContent
        {
            get 
            {
                bool result = this.IsDirty;
                foreach (var subcoll in this.GetMemberEntityCollections())
                {
                    if (subcoll.ContainsDirtyContents)
                    {
                        result = true;
                    }
                }
                return result;
            }
        }

But how can I get notification of this property ? (without generating memory leaks simple_smile Or is there a better way to achieve this ?

Thnx in advance simple_smile

Sven.

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 11-Jul-2012 12:01:22   

This seem to work for me, but it seems too easy and "too good to be true" ..what am I missing ?


        protected override void OnInitialized()
        {
            this.EntityContentsChanged += (sender, args) => OnPropertyChanged("HasDirtyContent");
            base.OnInitialized();
        }

        protected override void OnRelatedEntitySet(IEntityCore relatedEntity, string fieldName)
        {
            base.OnRelatedEntitySet(relatedEntity, fieldName);
            relatedEntity.EntityContentsChanged += (sender, args) => OnPropertyChanged("HasDirtyContent");
        }

        protected override void OnRelatedEntityUnset(IEntityCore relatedEntity, string fieldName)
        {
            base.OnRelatedEntityUnset(relatedEntity, fieldName);
            relatedEntity.EntityContentsChanged -= (sender, args) => OnPropertyChanged("HasDirtyContent");
        }

        public bool HasDirtyContent
        {
            get
            {
                bool result = this.IsDirty;
                foreach (var f in this.GetMemberEntityCollections())
                {
                    if (f.ContainsDirtyContents)
                    {
                        result = true;
                    }
                }
                return result;
            }
        }


Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 11-Jul-2012 20:39:58   

Looks good. I don't think there is something missing.