Let's say you are binding your object to a PropertyGrid, whenever a Property of your object get changed, it should fire a PropertyChanged event in the GUI thread, in order for the PropertyGrid to update properly.
It is your duty to marshal a Property setter to GUI thread. If you can have a link to any Control, use that Control to invoke. Otherwise, a general solution is create a dummy control at the beginning and use it for invoking.
public partial class ControlBase : UserControl
{
/// <summary>
/// Dummy control, use for invoking
/// </summary>
public static Control sDummyControl = null;
/// <summary>
/// Constructor
/// </summary>
public ControlBase()
{
InitializeComponent();
if (sDummyControl == null)
{
sDummyControl = new Control();
sDummyControl.Handle.ToString(); // Force create handle
}
}
}
In your parent object:
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PropertyChanged == null)
{
return;
}
Control c = ControlBase.sDummyControl;
if (c == null)
{
PropertyChanged(sender, e);
}
else
{
if (c.InvokeRequired)
{
c.BeginInvoke(new PropertyChangedEventHandler(RaisePropertyChanged), new object[] { sender, e });
}
else
{
PropertyChanged(sender, e);
}
}
}
In your object:
public string Name
{
get { return _name; }
set { _name = value; RaisePropertyChanged(this, new PropertyChangedEventArgs("Name")); }
}
HTH, Huy