C# 如何将 WPF 页面添加到 tabcontrol?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15589298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add WPF page to tabcontrol?
提问by Moataz Aahmed Mohammed
I have this main wpf window
我有这个主 wpf 窗口
and this WPF page
和这个 WPF 页面
I need to add this page to tabcontrol in main window
我需要将此页面添加到主窗口中的 tabcontrol
This is my OnRender method
这是我的 OnRender 方法
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
Page1 page1 = new Page1();
tabitem.Content = new Page1();
ISFirstRender = false;
}
base.OnRender(drawingContext);
}
after the application running I faced this exception while selecting the new tab
应用程序运行后,我在选择新选项卡时遇到此异常
I need to know how to add wpf page to existing tabcontroll
我需要知道如何将 wpf 页面添加到现有的 tabcontroll
采纳答案by keyboardP
If you want to add a new Page
, as opposed to a UserControl
, you can create a new Frame
object and place the page in there.
如果您想添加一个 new Page
,而不是一个UserControl
,您可以创建一个新Frame
对象并将页面放在那里。
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
Frame tabFrame = new Frame();
Page1 page1 = new Page1();
tabFrame.Content = page1;
tabitem.Content = tabFrame;
pan1.Items.Add(tabitem);
ISFirstRender = false;
}
回答by Hossein Narimani Rad
You can add user controls to the TabControl
. So go to the add new items and select the user control and make what you want (like what you have in the page). Then add an instance of that user control to the TabControl
.
您可以将用户控件添加到TabControl
. 因此,转到添加新项目并选择用户控件并制作您想要的内容(例如您在页面中拥有的内容)。然后将该用户控件的一个实例添加到TabControl
.
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
MyUserControl userControl = new MyUserControl();
tabitem.Content = userControl;
ISFirstRender = false;
}
base.OnRender(drawingContext);
}