對於對象=>控制更新:您的活動將不會引發 - 自動實現的屬性不關心INotifyPropertyChanged
- 你需要將其遷出:
public class Data : INotifyPropertyChanged {
int _value = 10;
public int Value {
get {return _value;}
set {
if(value != _value) {
_value = value;
OnPropertyChanged("Value");
}
}
}
protected virtual void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
if(handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
但對於其他(控制=> object),我希望你沒有正確的綁定設置。你應該結合的Data.Value
屬性,你需要在運行時告訴它你的Data
例如:
someBindingSource.DataSource = data;
或者,如果您是直接使用DataBindings
- 下面說明了它與上述類型的工作:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Data data = new Data();
data.PropertyChanged += delegate {
Debug.WriteLine("Value changed: " + data.Value);
};
Button btn;
NumericUpDown nud;
Form form = new Form {
Controls = {
(nud = new NumericUpDown()),
(btn = new Button {
Text = "Obj->Control",
Dock = DockStyle.Bottom })
}
};
nud.DataBindings.Add("Value", data, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);
btn.Click += delegate { data.Value++; };
Application.Run(form);
}
}