Search code examples
c#propertiesdependency-propertiesinotifypropertychanged

Update Property When Another Property Changed


I want to update Name property of a class whenever matching ID changes, I get the matching Name from web reference.

For example:

class Request 
{
    private int xID;

    public int XID 
    {
        get =>
        set 
        {
             xID = value;
             XName = value;
        }
   }

   private string xName;

   public string XName 
   {
       get => xName 
       set
       {
           xName = new FillUser(value).FullUserName   // the usage of the WebReference,
                                                      // create an object with all user credentials 
       }
    }

    public int YID { get; set; }
    public string YName { get; set; }
}

So I have multiple ID's and all of them has matching name and all of them will work the same.

Except ID value will be change depending on the prop.

Looking for best practice way to solve this problem - thanks a lot.


Solution

  • The solution that I find It's creating custom Attribute for those property, and then under that category you multiple ways.
    You can send argument to the prop attribute with name of the dependent attribute,
    Or generic method, and etc.