gpt4 book ai didi

c# - WPF,从 UserControl 更新主窗口中的状态栏

转载 作者:太空狗 更新时间:2023-10-29 23:18:56 24 4
gpt4 key购买 nike

我的主窗口中有一个 StatusBar,我的主窗口中还有一个 UserControl 的副本。在我的 UserControl 中的事件处理程序中,我想更新主窗口中的 StatusBar。这样做的最佳方法是什么?有没有办法在 UserControl 的事件处理程序中从 object senderRoutedEventArgs e 访问我的主窗口实例?

编辑:感谢lukas's answerthis tutorial ,我想出了以下解决方案:

添加到我的 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");
}

最佳答案

我会通过我自己的委托(delegate)或定义向 UserControl 添加一个事件

public event UpdateStatusBar UpdateBar;

然后通过在用户控件(或您使用的其他东西)中单击按钮来提升它

    private void UserContolButton_Click(object sender, RoutedEventArgs e)
{
if(UpdateBar != null)
UpdateBar(); // send here the message
}

我假设你在主窗口中有一个 UserControl 实例在构造函数中

 myUserControl.UpdateBar += MyMethodWhichUpdatesStatusBar();

关于c# - WPF,从 UserControl 更新主窗口中的状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3460531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com