首页 > 解决方案 > 如何从其他 XAML 在 UWP 透视中添加元素

问题描述

我有这个带有一个 Pivot 元素的 XAML 代码。我想添加元素而不是TextBlock其他 XAML 中的元素。这是我的代码:

<Grid>
    <Grid RequestedTheme="Default">
        <Grid.RowDefinitions>
            <RowDefinition Height="23*"/>
            <RowDefinition Height="978*"/>
        </Grid.RowDefinitions>
        <Pivot Grid.RowSpan="2">
            <PivotItem Header="All">
                <TextBlock Text="All works here" />
            </PivotItem>
            <PivotItem Header="Important">
                <TextBlock Text="Important works goes here" />
            </PivotItem>
            <PivotItem Header="Pending">
                <TextBlock Text="Pending works goes here" />
            </PivotItem>
            <PivotItem Header="Done">
                <TextBlock Text="Done works goes here" />
            </PivotItem>
        </Pivot>
    </Grid>
</Grid>

有人可以告诉我我该怎么做吗?

标签: c#xamluwp

解决方案


根据您的评论,您希望将自定义控件添加到 PivotItem 中。我不知道您的自定义控件是什么样的,所以我自己制作了一个自定义控件来向您展示它应该如何工作。您可以将其替换为您自己的自定义控件。

主页代码:

 <Grid RequestedTheme="Default">
        <Grid.RowDefinitions>
            <RowDefinition Height="23*"/>
            <RowDefinition Height="978*"/>
        </Grid.RowDefinitions>
        <Pivot Grid.RowSpan="2">
            <PivotItem Header="All">
                <TextBlock Text="All works here" />
            </PivotItem>
            <PivotItem Header="Important">
                <!--this is the custom control I made-->
                <local:CustomTextBox x:Name="MycustomControl"/>
            </PivotItem>
            <PivotItem Header="Pending">
                <TextBlock Text="Pending works goes here" />
            </PivotItem>
            <PivotItem Header="Done">
                <TextBlock Text="Done works goes here" />
            </PivotItem>
        </Pivot>
    </Grid>

自定义控件的代码:

  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox x:Name="InputBox" Width="600"/>
    <Button x:Name="ClickButton" Content="Please Click" Click="ClickButton_Click" Grid.Row="1"/>
    <TextBlock x:Name="DisplayTextBlock" Grid.Row="2"/>
</Grid>

它是一个简单的自定义控件,它结合了一个 TextBox、一个按钮和一个 TextBlock。

结果如下所示: 在此处输入图像描述

如果您仍有疑问,请告诉我


推荐阅读