c# 访问器不是通过引用调用的
c# accessor not called by reference
我的问题如下。我有一个 class 人:
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;
当我这样做时,我的 属性 的 setter 运行得很好。这也意味着
this.signals = ChartConverter(value);
被执行。如果我更改引用的对象,"this.dictionary" 值出现在 "myGraphView.dictionary" 中,但 setter 不会执行,我的转换也不会发生。
我该如何解决?我的 class ObservableChartDictionary 也实现了 INotifyPropertyChanged,但是 "myGraphView.dictionary" 中也没有引发该事件。请帮忙!
这可能是因为您正在以某种方式更改字典,例如
this.dictionary.someproperty ... or this.dictionary.someMethod(...)
这样 setter 属性 就不会触发。它只是改变了你的字典的内容,其他引用它的人看到了变化。
this.dictionary = 某些东西触发了集合 属性。
如果您想检测更改,此代码可能会有所帮助:
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;
}
observableDictionary 是专门为 graphView 设计的,对吧?如果你想连接它们,为什么不直接 link 你的 ObservableChartDictionary 实例到一个实际的图形视图..
public class ObservableChartDictionary
{
public GraphView linkedGraph { get; set; }
public ObservableChartDictionary(GraphView linkedGraph)
{
this.linkedGraph = linkedGraph;
}
//...
}
然后您可以在字典更改时更新字典本身的值(例如,我 属性 通知更改):
if (linkedGraph != null)
{
linkedGraph .signals = ChartConverter(this);
}
所以它会在您每次添加条目或更改内容时更新图表。
您甚至可以嵌套 class 以显示它们紧密相连。
我的问题如下。我有一个 class 人:
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;
当我这样做时,我的 属性 的 setter 运行得很好。这也意味着
this.signals = ChartConverter(value);
被执行。如果我更改引用的对象,"this.dictionary" 值出现在 "myGraphView.dictionary" 中,但 setter 不会执行,我的转换也不会发生。
我该如何解决?我的 class ObservableChartDictionary 也实现了 INotifyPropertyChanged,但是 "myGraphView.dictionary" 中也没有引发该事件。请帮忙!
这可能是因为您正在以某种方式更改字典,例如
this.dictionary.someproperty ... or this.dictionary.someMethod(...)
这样 setter 属性 就不会触发。它只是改变了你的字典的内容,其他引用它的人看到了变化。
this.dictionary = 某些东西触发了集合 属性。
如果您想检测更改,此代码可能会有所帮助:
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;
}
observableDictionary 是专门为 graphView 设计的,对吧?如果你想连接它们,为什么不直接 link 你的 ObservableChartDictionary 实例到一个实际的图形视图..
public class ObservableChartDictionary
{
public GraphView linkedGraph { get; set; }
public ObservableChartDictionary(GraphView linkedGraph)
{
this.linkedGraph = linkedGraph;
}
//...
}
然后您可以在字典更改时更新字典本身的值(例如,我 属性 通知更改):
if (linkedGraph != null)
{
linkedGraph .signals = ChartConverter(this);
}
所以它会在您每次添加条目或更改内容时更新图表。
您甚至可以嵌套 class 以显示它们紧密相连。