WPF,从 UserControl 中更新主窗口中的状态栏
我的主窗口中有一个 StatusBar
,并且主窗口中还有一个 UserControl
的副本。在我的 UserControl
的事件处理程序中,我想更新主窗口中的 StatusBar
。这样做的最好方法是什么?有没有办法在 UserControl
的事件处理程序中从 object sender
或 RoulatedEventArgs e
访问我的主窗口实例?
编辑:感谢卢卡斯的回答和本教程,我想出了以下解决方案:
添加到我的 UserControl
:
public delegate void UpdateStatusBarEventHandler(string message);
public event UpdateStatusBarEventHandler UpdateStatusBar;
添加到我的主窗口的构造函数,在 InitializeComponent
之后:
uct_requiredFields.UpdateStatusBar += updateStatusBar;
我将此方法添加到我的主窗口:
private void updateStatusBar(string message)
{
sti_mainStatus.Content = message;
}
然后,在我的 UserControl
中,我可以执行以下操作来更新状态栏:
if (null != UpdateStatusBar)
{
UpdateStatusBar("woot, message");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将通过我自己的委托或定义向 UserControl 添加一个事件
,然后通过单击 UserControl (或您使用的其他东西)中的按钮来启动它
我假设您在主窗口中有一个 UserControl 的实例
在构造函数中
I would add an event to UserControl via my own delegate or defined
and then rise it via button click in UserControl ( or other thing that u use)
I assume u have an instance of UserControl in the main window
in contructor