如何在类内的类中的属性上使用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
}
你们有什么想法?如果您想要一个示例应用程序,我可以帮忙。
没有找到相关结果
已邀请:
4 个回复
刷遍派戳
请记住,如果您放弃了AspectRatio对象(如果对象引用发生了变化,而不仅仅是该对象的value属性发生了变化),则应该取消订阅该事件。
厦惫
babsoft
室邢