I'm having troubles with a data binding between a label text and a class property. I have a class like this:
public ref class TestClass : public INotifyPropertyChanged
{
public:
virtual event PropertyChangedEventHandler ^PropertyChanged
{
void add(PropertyChangedEventHandler^ value) { this->propertyChanged += value; }
void remove(PropertyChangedEventHandler^ value) { this->propertyChanged -= value; }
};
virtual void NotifyPropertyChanged(String ^info) override
{
propertyChanged(this, gcnew PropertyChangedEventArgs(info));
};
property unsigned int ToW
{
void set(unsigned int tow_){
tow = tow_;
NotifyPropertyChanged("ToW");
}
unsigned int get()
{
return tow;
}
};
private:
unsigned int tow;
event PropertyChangedEventHandler ^propertyChanged;
};
And I have a window where I have label which I want it to be automatically updated as the property "ToW" in the class above is changed. So I define the following databinding:
this->labelToW->DataBindings->Add(gcnew Binding("Text", testClass, "ToW"));
"testClass" is an instance of TestClass.
However, when I update the "ToW" property, the label doesn't get updated and I get an error like this:
"Cross-thread operation not valid: Control 'labelToW' accessed from a thread other than the thread it was created on."
I guess the problem is that ToW property is updated by another thread I run to listen to a socket.
Is there any workaround to this problem
Am I missing something
Thanks,
Spulit