开发者

Raise PropertyChanged on custom properties in EntityObject

I have several custom calculated properties on an EntityObject. I would like to fire the PropertyChanged event to notify all bindings. This does not work however, and it throws an argumentexception when I use ReportPropertyChanged:

De eigenschap Name heeft geen geldige entiteitstoewijzing op het entiteitsobject. Zie de documentatie van Entity Framework voor meer informat开发者_运维知识库ie.

Which means than Name is not an entity property and I should look in the Entity Framework documentation for more information. Is there any way to call the event and have the Entity Framework ignore it?

code example:

partial class Preset
{
    public string Name
    {
        get
        {
            if (this.SystemPreset)
                return Translate("preset_" + this.Name_Original + "_name");
            else
                return this.Name_Original;
        }
        set
        {
            if (!value != this.Name_Internal)
            {
                this.Name_Internal = value;
                ReportPropertyChanged(Name);
            }
        }
    }
}

The property Name is a custom property. The entity framework will throw an exception when I try to fire PropertyChanged through the method ReportPropertyChanged in EntityObject.


You should be able to just call OnPropertyChanged("YourPropertyName") (msdn) from your custom property setter and WPF will pick it up.

This works for me in Visual Studio 2010, .NET 4.0 talking to an SQLServer CE database (I think it's entity framework 4 but I'm not sure).


ReportPropertyChanged is not for your use it's for the EF to notify ObjectStateManager to change the CurrentValues for that object so that it can keep track of the changes and generate appropriate SQL commands at later point when we call SaveChanges on ObjectContext.

That said, every scalar property of every entity has its own version of 2 partial methods that is there for you to implement: On[Property]Changed and On[Property]Changing.
There is no default implementation for PropertyChanging and PropertyChanged; only a declaration. This provides you the opportunity to execute custom logic as the property is about to change (PropertyChanging) as well as just after the property value has changed (PropertyChanged) and you can use them to update the value of your custom calculated properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜