我有这些:
- 带有标签的表格
- “商务”类
- 一个类“TimerHelper”
我想在更新属性时也MyTime
更新文本框
我的“商务”课程如下所示:
public class MyBusinessClass : INotifyPropertyChanged
{
public void MakeSound(object sender, ElapsedEventArgs e)
{
// I change MyTime here
}
public event PropertyChangedEventHandler PropertyChanged;
private int myTime;
public int MyTime
{
get { return myTime; }
set
{
myTime= value;
InvokePropertyChanged(new PropertyChangedEventArgs("MyTime"));
}
}
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
“TimerHelper”看起来像这样:
public class TimerHelper
{
private Timer _timer;
public void Run()
{
_timer = new Timer(1000);
_timer.Enabled = true;
_timer.Elapsed += new ElapsedEventHandler(MyBusinessClass.MakeSound);
}
}
在表格中,我尝试了这个:
myTextBox.DataBindings.Add("Text", new MyBusinessClass(), "MyTime");
但我在“InvokePropertyChanged”方法中遇到异常
我试过这个:
Invoke((MethodInvoker)delegate { myTextBox.Text = new MyBusinessClass().MyTime; });
但文本框永远不会更新