I have a StatusBar
in my main window, and I also have a copy of a UserControl
in my main window. From within event handlers in my UserControl
, I want to update the StatusBar
in the main window. What would be the best way of doing this? Is there a way of getting access to the instance of my main window from object sender
or RoutedEventArgs e
in an event handler in UserControl
?
Edit: thanks to lukas's answer and this tutorial, I came up with the following solution:
Added to my UserControl
:
public delegate void UpdateStatusBarEventHandler(string message);
public event UpdateStatusBarEventHandler UpdateStatusBar;
Added to my main window's constructor, after InitializeComponent
:
uct_requiredFields.UpdateStatusBar += updateStatusBar;
And I added this method to my main window:
private void updateStatusBar(string message)
{
sti_mainStatus.Content = message;
}
Then, from within my UserControl
, I can do the following to update the status bar:
if (null != UpdateStatusBar)
{
UpdateStatusBar("woot, message");
}