Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 153773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:51:38+00:00 2026-05-11T09:51:38+00:00

I’m developing an application in Silverlight2 and trying to follow the Model-View-ViewModel pattern. I

  • 0

I’m developing an application in Silverlight2 and trying to follow the Model-View-ViewModel pattern. I am binding the IsEnabled property on some controls to a boolean property on the ViewModel.

I’m running into problems when those properties are derived from other properties. Let’s say I have a Save button that I only want to be enabled when it’s possible to save (data has been loaded, and we’re currently not busy doing stuff in the database).

So I have a couple of properties like this:

    private bool m_DatabaseBusy;     public bool DatabaseBusy     {         get { return m_DatabaseBusy; }         set         {             if (m_DatabaseBusy != value)             {                 m_DatabaseBusy = value;                 OnPropertyChanged('DatabaseBusy');             }         }     }      private bool m_IsLoaded;     public bool IsLoaded     {         get { return m_IsLoaded; }         set         {             if (m_IsLoaded != value)             {                 m_IsLoaded = value;                 OnPropertyChanged('IsLoaded');             }         }     } 

Now what I want to do is this:

public bool CanSave {      get { return this.IsLoaded && !this.DatabaseBusy; } } 

But note the lack of property-changed notification.

So the question is: What is a clean way of exposing a single boolean property I can bind to, but is calculated instead of being explicitly set and provides notification so the UI can update correctly?

EDIT: Thanks for the help everyone – I got it going and had a go at making a custom attribute. I’m posting the source here in case anyone’s interested. I’m sure it could be done in a cleaner way, so if you see any flaws, add a comment or an answer.

Basically what I did was made an interface that defined a list of key-value pairs to hold what properties depended on other properties:

public interface INotifyDependentPropertyChanged {     // key,value = parent_property_name, child_property_name, where child depends on parent.     List<KeyValuePair<string, string>> DependentPropertyList{get;} } 

I then made the attribute to go on each property:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)] public class NotifyDependsOnAttribute : Attribute {     public string DependsOn { get; set; }     public NotifyDependsOnAttribute(string dependsOn)     {         this.DependsOn = dependsOn;     }      public static void BuildDependentPropertyList(object obj)     {         if (obj == null)         {             throw new ArgumentNullException('obj');         }          var obj_interface = (obj as INotifyDependentPropertyChanged);          if (obj_interface == null)         {             throw new Exception(string.Format('Type {0} does not implement INotifyDependentPropertyChanged.',obj.GetType().Name));         }          obj_interface.DependentPropertyList.Clear();          // Build the list of dependent properties.         foreach (var property in obj.GetType().GetProperties())         {             // Find all of our attributes (may be multiple).             var attributeArray = (NotifyDependsOnAttribute[])property.GetCustomAttributes(typeof(NotifyDependsOnAttribute), false);              foreach (var attribute in attributeArray)             {                 obj_interface.DependentPropertyList.Add(new KeyValuePair<string, string>(attribute.DependsOn, property.Name));             }         }     } } 

The attribute itself only stores a single string. You can define multiple dependencies per property. The guts of the attribute is in the BuildDependentPropertyList static function. You have to call this in the constructor of your class. (Anyone know if there’s a way to do this via a class/constructor attribute?) In my case all this is hidden away in a base class, so in the subclasses you just put the attributes on the properties. Then you modify your OnPropertyChanged equivalent to look for any dependencies. Here’s my ViewModel base class as an example:

public class ViewModel : INotifyPropertyChanged, INotifyDependentPropertyChanged {     public event PropertyChangedEventHandler PropertyChanged;     protected virtual void OnPropertyChanged(string propertyname)     {         if (PropertyChanged != null)         {             PropertyChanged(this, new PropertyChangedEventArgs(propertyname));              // fire for dependent properties             foreach (var p in this.DependentPropertyList.Where((x) => x.Key.Equals(propertyname)))             {                 PropertyChanged(this, new PropertyChangedEventArgs(p.Value));             }         }     }      private List<KeyValuePair<string, string>> m_DependentPropertyList = new List<KeyValuePair<string, string>>();     public List<KeyValuePair<string, string>> DependentPropertyList     {         get { return m_DependentPropertyList; }     }      public ViewModel()     {         NotifyDependsOnAttribute.BuildDependentPropertyList(this);     }  } 

Finally, you set the attributes on the affected properties. I like this way because the derived property holds the properties it depends on, rather than the other way around.

    [NotifyDependsOn('Session')]     [NotifyDependsOn('DatabaseBusy')]     public bool SaveEnabled     {         get { return !this.Session.IsLocked && !this.DatabaseBusy; }     } 

The big caveat here is that it only works when the other properties are members of the current class. In the example above, if this.Session.IsLocked changes, the notification doesnt get through. The way I get around this is to subscribe to this.Session.NotifyPropertyChanged and fire PropertyChanged for ‘Session’. (Yes, this would result in events firing where they didnt need to)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:51:38+00:00Added an answer on May 11, 2026 at 9:51 am

    The traditional way to do this is to add an OnPropertyChanged call to each of the properties that might affect your calculated one, like this:

    public bool IsLoaded {     get { return m_IsLoaded; }     set     {         if (m_IsLoaded != value)         {             m_IsLoaded = value;             OnPropertyChanged('IsLoaded');             OnPropertyChanged('CanSave');         }     } } 

    This can get a bit messy (if, for example, your calculation in CanSave changes).

    One (cleaner? I don’t know) way to get around this would be to override OnPropertyChanged and make the call there:

    protected override void OnPropertyChanged(string propertyName) {     base.OnPropertyChanged(propertyName);     if (propertyName == 'IsLoaded' /* || propertyName == etc */)     {         base.OnPropertyChanged('CanSave');     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use UNION: UNION is used to combine the… May 11, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer That is rather odd; obviously the semantics of this are… May 11, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer If you have cygwin installed, from it's bash shell, run… May 11, 2026 at 7:57 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.