如何在类内的类中的属性上使用INotifyPropertyChanged?

| 我的问题似乎是“范围”,尽管我不确定这是正确的术语。当设置自定义对象中的属性时,我想通知只读列表以重新评估自身。我相信它根本不知道它的存在。也许有一种我无法想到的简便方法,但是我正在空白。 我发现这很难用语言表达,因此这里是简化的代码,并附有我对可能发生的情况的评论。 我要绑定到的对象内的属性:
private CvarAspectRatios _aspectRatio = new CvarAspectRatios(\"none\", GetRatio());
public CvarAspectRatios AspectRatio
{
    get { return _aspectRatio; }
    set
    {                                                 // This setter never gets hit since I bind to this 
        if (value != null)                            // object\'s \'Value\' property now.
        {
            _aspectRatio = value;
            NotifyPropertyChanged(\"AspectRatio\");
            NotifyPropertyChanged(\"ResolutionList\");  // I want to inform ResolutionList
        }                                             // that it needs to repopulate based
    }                                                 // on this property: AspectRatio
}

private ResolutionCollection _resolutionList = ResolutionCollection.GetResolutionCollection();
public ResolutionCollection ResolutionList
{
   get 
   {
       ResolutionCollection list = new ResolutionCollection();
       if (AspectRatio != null && AspectRatio.Value != null)
       {
           foreach (Resolutions res in _resolutionList.Where(i => i.Compatibility == AspectRatio.Value.Compatibility))
           {
               list.Add(res);
           }
           return list;
       }
       return _resolutionList;
   }
}
CvarAspectRatios类别:
public class CVarAspectRatios : INotifyPropertyChanged
{
    private string _defaultValue;
    public string DefaultValue
    {
        get { return _defaultValue; }
        set { _defaultValue = value; NotifyPropertyChanged(\"DefaultValue\"); }
    }

    private AspectRatios _value;
    public AspectRatios Value
    {
        get { return _value; }
        set 
        { 
            _value = value; 
            NotifyPropertyChanged(\"Value\"); 
            NotifyPropertyChanged(\"ResolutionList\");  // This value gets set, and I\'d like for ResolutionList to update
        }                                             // but it cannot find ResolutionList. No errors or anything. Just
    }                                                 // no update.

    public AspectRatios() { }

    public AspectRatios(string defaultValue, AspectRatios val)
    {
        DefaultValue = defaultValue;
        Value = val;
    }

    // Implementation of INotifyPropertyChanged snipped out here
}
你们有什么想法?如果您想要一个示例应用程序,我可以帮忙。     
已邀请:
        由于CVarAspectRatios实现了INotifyPropertyChanged,因此您可以让viewmodel类订阅AspectRatio的PropertyChanged事件。
public class YourViewModel 
{
    public YourViewModel()
    {
        AspectRatio.PropertyChanged += AspectRatio_PropertyChanged;
    }

    void AspectRatio_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == \"Value\")
            NotifyPropertyChanged(\"ResolutionList\");
    }        
}
请记住,如果您放弃了AspectRatio对象(如果对象引用发生了变化,而不仅仅是该对象的value属性发生了变化),则应该取消订阅该事件。     
        要将现有代码转换为应该起作用的代码:
private CvarAspectRatios _aspectRatio; //No field initialization because that would not attach event handler, you could do it though and take care of the handler alone in the ctor
public CvarAspectRatios AspectRatio
{
    get { return _aspectRatio; }
    set
    {
        if (_aspectRatio != value) // WTH @ \"value != null\"
        {
            _aspectRatio.PropertyChanged -= AspectRatio_PropertyChanged;
            _aspectRatio = value;
            _aspectRatio.PropertyChanged += new PropertyChangedEventHandler(AspectRatio_PropertyChanged);
            NotifyPropertyChanged(\"AspectRatio\");
        }
    }
}

void AspectRatio_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == \"Value\")
    {
        NotifyPropertyChanged(\"ResolutionList\");
    }
}
    
        您为什么不排除将ѭ4重新填充为一个单独的私有方法,该方法会从ѭ5the的设置者处调用呢?     
        如果列表需要基于更改后的属性进行更新,则该列表(或列表管理器对象,以进行更好的封装)通常需要预订承载该属性的对象的PropertyChanged事件。如果列表本身是同一对象的属性,则在这种情况下,属性的设置方法调用更新列表的方法会更简单,更精简。     

要回复问题请先登录注册