我的問題似乎是「範圍」,但我不確定這是正確的術語。我想通知一個只讀列表,以便在設置自定義對象中的屬性時重新評估自己。我相信它根本不知道它的存在。也許有一個簡單的辦法可以解決這個問題,我想不出來,但是我畫了一個空白。如何在類的屬性中使用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
}
你是什麼人想的?如果你想要一個示例應用程序,我可以鞭打一個。
先生,這是非常簡單,它很好。謝謝! – erodewald 2011-05-27 17:19:40
當我這樣綁定到DataGridView時,我得到一個InvalidOperationException。 'BindingSource不能是它自己的數據源。不要將DataSource和DataMember屬性設置爲引用BindingSource的值。' – mphair 2011-06-23 21:50:47
我不確定您的錯誤是否由上述實現引起,因爲沒有設置任何內容。 – Adrian 2011-06-23 22:28:34