按钮位于 tabitem 下(在 tabcontrol 内),但处理程序需要位于主 window
Button located under tabitem (within tabcontrol) but the handler needs to be in the main window
我有一个主 windows,其中包含一些控件,包括一个选项卡控件。选项卡控件本身有多个 tabitem。每个选项卡项目都有独特的设计。当用户单击其中一个选项卡项中的按钮时,我希望事件处理程序位于主 window 的 .cs 文件中。我想不出一个办法,谁能启发我?
我的语法可能不完美,但是这个呢:
public interface ITabControlWithNotifications
{
void OnButtonClicked(...);
}
public sealed class TabControlWithNotifications : TabControl, ITabControlWithNotifications
{
public void OnButtonClicked(...)
{
...
}
}
public sealed class TabPageWithNotifications : TabPage
{
private readonly ITabControlWithNotifications parentTabControl;
public TabPageWithNotifications(ITabControlWithNotifications tabControlWithNotifications)
{
this.parentTabControl = tabControlWithNotifications;
this.InitialzeComponents();
}
... ClickEventHandler(...)
{
this.parentTabControl.OnButtonClicked(...);
}
}
public sealed class TabControlFactory
{
public void Build()
{
var parentTabControl = new TabControlWithNotifications();
var tabPage1 = new TabPageWithNotifications(parentTabControl);
var tabPage2 = new TabPageWithNotifications(parentTabControl);
var tabPage3 = new TabPageWithNotifications(parentTabControl);
parentTabControl.Controls.Add(tabPage1);
parentTabControl.Controls.Add(tabPage2);
parentTabControl.Controls.Add(tabPage3);
}
}
我有一个主 windows,其中包含一些控件,包括一个选项卡控件。选项卡控件本身有多个 tabitem。每个选项卡项目都有独特的设计。当用户单击其中一个选项卡项中的按钮时,我希望事件处理程序位于主 window 的 .cs 文件中。我想不出一个办法,谁能启发我?
我的语法可能不完美,但是这个呢:
public interface ITabControlWithNotifications
{
void OnButtonClicked(...);
}
public sealed class TabControlWithNotifications : TabControl, ITabControlWithNotifications
{
public void OnButtonClicked(...)
{
...
}
}
public sealed class TabPageWithNotifications : TabPage
{
private readonly ITabControlWithNotifications parentTabControl;
public TabPageWithNotifications(ITabControlWithNotifications tabControlWithNotifications)
{
this.parentTabControl = tabControlWithNotifications;
this.InitialzeComponents();
}
... ClickEventHandler(...)
{
this.parentTabControl.OnButtonClicked(...);
}
}
public sealed class TabControlFactory
{
public void Build()
{
var parentTabControl = new TabControlWithNotifications();
var tabPage1 = new TabPageWithNotifications(parentTabControl);
var tabPage2 = new TabPageWithNotifications(parentTabControl);
var tabPage3 = new TabPageWithNotifications(parentTabControl);
parentTabControl.Controls.Add(tabPage1);
parentTabControl.Controls.Add(tabPage2);
parentTabControl.Controls.Add(tabPage3);
}
}