简体   繁体   English

将WPF中的选项卡内容分配给页面

[英]Assign tab content in WPF to a page

I am trying to add dynamic tabs to my application. 我正在尝试向我的应用程序添加动态选项卡。 Right now if I click a button, it will open a new page. 现在,如果我点击一个按钮,它将打开一个新页面。 What I want is to open this page in a new tab. 我想要的是在新标签页中打开此页面。 But when I set up the tab content to a page , the code complains. 但是当我将标签内容设置到页面时,代码会抱怨。 I wanna do something like this 我想做这样的事情

private void bttnGoToClientsOnClick(object sender, RoutedEventArgs e)
        {
            var content = new TextBlock();
            TabItem tab = new TabItem();
            tab.Header = "Search Page";
            SearchPage sp = new SearchPage();
            tab.Content = sp;
            tabControl.Items.Add(tab);
            this.NavigationService.Navigate(sp);
        }

is there any way I can convert my page to usercontrol or cast it as user control 有什么办法可以将我的页面转换为usercontrol或将其转换为用户控件

Thank you! 谢谢!

But when I set up the tab content to a page , the code complains. 但是当我将标签内容设置到页面时,代码会抱怨。

It wouldn't hurt if you were more specific here :) 如果你在这里更具体,它不会受到伤害:)

What is SearchPage class? 什么是SearchPage类? It doesn't seem to be the part of the WPF framework. 它似乎不是WPF框架的一部分。 I googled it up on the http://www.intersoftpt.com/ website. 我在http://www.intersoftpt.com/网站上搜索了它。 Is that it? 是吗?

TabItem.Content needs to be of ContentControl type, which SearchPage - apparently - is not. TabItem.Content需要是ContentControl类型, SearchPage - 显然 - 不是。 I'm sure you need to embed this SearchPage object in some control presenter, such as a panel, before you can assign it to TabItem.Content . 我确定您需要将此SearchPage对象嵌入到某个控件展示器(例如面板)中,然后才能将其分配给TabItem.Content


Update: 更新:

Try this, then: 试试这个,然后:

        TabItem tab = new TabItem();
        tab.Header = "Search Page";
        SearchPage sp = new SearchPage();
        this.NavigationService.Navigate(sp);
        // ---------------------------------------------------- 
        var frame = new Frame(); // !
        frame.Navigate(sp);  // !
        tab.Content = frame; // !
        // ---------------------------------------------------- 
        tabControl.Items.Add(tab);

While I believe this should work, I haven't tested it. 虽然我认为这应该有用,但我还没有测试过。 Please let me know if it doesn't do the trick. 如果不能解决问题,请告诉我。

You can always create your own UserControls, directly in the XAML definition (even if they are partial pages or windows). 您始终可以直接在XAML定义中创建自己的UserControl(即使它们是部分页面或窗口)。

In this example I assume that your SearchClass is defined in the [YourProject].Model namespace (where [YourProject] is the name of your project) 在这个例子中,我假设您的SearchClass是在[YourProject] .Model命名空间中定义的(其中[YourProject]是项目的名称)

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:search="clr-namespace:[YourProject].Model">
    <search:SearchClass>
        <!--<Grid>
            ...ANYTHING YOU WANT HERE ! ...
        </Grid>-->
    </search:SearchClass>
</UserControl>

Now you can create an instance of the UserControl, even in XAML or in code-behind (remember only to declare the namespaces correctly!): 现在,您可以创建UserControl的实例,即使在XAML或代码隐藏中也是如此(请记住仅正确声明命名空间!):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:ctrls="clr-namespace:WpfApplication1"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <UserControl1 />
    </Grid>
</Window>

...and this my code-behind... ......这是我的代码隐藏......

UserControl1 myControl = new UserControl1();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM