简体   繁体   English

另一个属性更改时更新属性

[英]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.我想在匹配ID更改时更新 class 的Name属性,我从 web 参考中获取匹配的Name

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.所以我有多个 ID,它们都有匹配的名称,并且它们都将工作相同。

Except ID value will be change depending on the prop.除了 ID 值会根据道具而改变。

Looking for best practice way to solve this problem - thanks a lot.寻找解决这个问题的最佳实践方法 - 非常感谢。

You should not do XName = value;你不应该做XName = value;

You write a method say ChangeId and then put all the logic in it.你写一个方法说 ChangeId,然后把所有的逻辑都放进去。 The other IDs props should remain get only.其他 ID 道具应保持仅获取。

void Changex(into xid)
{
    xId = xid;
    xName = Full User.GetUserNameById(xid);
}

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,您可以使用依赖属性的名称将参数发送到 prop 属性,
Or generic method, and etc.或泛型方法等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 Prism 中的另一个属性发生变化时更新一个属性 - Update a property when another property changes in Prism 当属性更改时,DataGridCheckBoxColumn 不会在 MVVM 中更新 - DataGridCheckBoxColumn does not update in MVVM when property is changed 更改模型中的不同属性时更新实体框架属性 - Update Entity Framework property when different property in model is changed 当先决条件属性在另一个类中更改时,为依赖属性提高PropertyChanged? - Raising PropertyChanged for a dependent property, when a prerequisite property is changed in another class? 在属性更改时在另一个类中通知属性? - Notify property in another class when property has changed? 当 ObservableCollection 中的 object 属性发生变化时,通知另一个属性 CommunityToolkit Mvvm - When object property in ObservableCollection changed, notify another property, CommunityToolkit Mvvm 有没有办法只将当前属性值分配给另一个属性,以便以后更改值时它不会更新? UWP C# - Is there a way to assign only current property value to another property so that it won't update when the value is later changed? UWP C# 更改BindingList中的属性后更新对象属性 - Update object property after property in BindingList is changed 当另一个类的属性更改时,如何提高属性更改? C# - How to raise property changed when property in another class is changed? c# 如何在MVVM中更改属性时更新UI更改 - How to update UI change when property changed in MVVM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM