我有一个主窗口,其中包含一些控件,包括tabcontrol。选项卡控件本身有多个tabitems。每个标签项都有独特的设计。当用户单击其中一个选项卡项中的按钮时,我希望事件处理程序位于主窗口的.cs文件中。我无法找到一种方法来做到这一点,任何人都可以启发我吗?
答案 0 :(得分:0)
我的语法可能不完美,但是这个:
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);
}
}