将页面加载到 ContentControl [英] Load a Page into a ContentControl

查看:24
本文介绍了将页面加载到 ContentControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a ContentControl where I want to load the page myPage2. My XAML Codefrom this page looks like this:

<Page x:Class="ExampleApp.myPage2">
    <Grid x:Name="Content" Height="651" Width="941" Background="White">
        ...
        ...
    </Grid>
</Page>

I know that I can load a resource from a page with this Code:

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
    var contentControl = (ContentControl)container;
    return (DataTemplate) contentControl.Resources[templateKey];
}

My problem now is that I can't load a page like above with this code. I have to write this:

<Page x:Class="ExampleApp.myPage2">
    <Page.Resources> 
        <DataTemplate x:Key="Test">       
            <Grid x:Name="Content" Height="651" Width="941" Background="White">
                ...
                ...
            </Grid>
        </DataTemplate>
    </Page.Resources>
</Page>

And then I can load the page with the same Code from above with templateKey="Test". But the main problem is that I want to use the first declaration of the page and do not want to use <Page.Resources> <DataTemplate x:Key="Test"> and so on. I want to load the site direcly from the first declaration (first code in this post). How can I create a DataTemplate directly from a page? Or is there an other way to load a page into a ContentControl?

解决方案

There is no reason to use a Page within a ContentControl. A Page is a subclass of the UserControl class that adds support for being used within a Frame control to support navigation, back stack/history, etc. You should probably replace Page with UserControl in XAML and code behind, so you would end up with something like this:

<UserControl x:Class="ExampleApp.myControl2">
    <Grid x:Name="Content" Height="651" Width="941" Background="White">
        ...
        ...
    </Grid>
</UserControl>

You can put the UserControl itself in a DataTemplate if you want to use it as a DataTemplate in a ContentControl:

<ContentControl
    xmlns:controls="using:ExampleApp">
    <ContentControl.Resources>
        <DataTemplate
            x:Key="Test">
            <controls:myControl2 />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

这篇关于将页面加载到 ContentControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆