我的问题如下。我有一个有经验的课程:
public partial class GraphView : UserControl
{
private ObservableChartDictionary _dictionary;
public ObservableChartDictionary dictionary
{
get
{
return _dictionary;
}
set
{
this._dictionary = value;
this.signals = ChartConverter(value);
}
}
}
我使这个属性后来等于另一个对象。
myGraphView.dictionary = this.dictionary;
当我这样做时,我的财产的设定者运行良好。这也意味着
this.signals = ChartConverter(value);
已执行。如果我更改引用的对象,“this.dictionary”的值将出现在“myGraphView.dictionary”中,但是setter不会执行,并且我的转换不会发生。
我该如何解决这个问题?我的类ObservableChartDictionary也实现了INotifyPropertyChanged,但事件也没有在“myGraphView.dictionary”中引发。请帮忙!
答案 0 :(得分:1)
这可能是因为你正在以某种方式改变字典
this.dictionary.someproperty ... or this.dictionary.someMethod(...)
这样,setter属性就不会触发。它只是更改了字典的内容,而另一个对它的引用会看到更改。
this.dictionary =触发set属性。
如果您想检测更改,此代码可能有所帮助:
public class ObservableChartDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyPropertyChanged
{
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public TValue this[TKey key]
{
get { return this[key]; }
set
{
base[key]= value;
OnPropertyChanged(key.ToString());
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
答案 1 :(得分:0)
observableDictionary仅适用于graphView,对吗?如果你想连接它们,为什么不把你的ObservableChartDictionary实例链接到一个实际的图形视图..
public class ObservableChartDictionary
{
public GraphView linkedGraph { get; set; }
public ObservableChartDictionary(GraphView linkedGraph)
{
this.linkedGraph = linkedGraph;
}
//...
}
然后,只要字典发生更改,您就可以更新字典本身的值(例如,我更改了属性通知):
if (linkedGraph != null)
{
linkedGraph .signals = ChartConverter(this);
}
因此,每次添加条目或更改内容时,它都会更新图表。
你甚至可以创建一个嵌套类来表明它们是紧密相连的。