如何使用< TabControl.ContentTemplate>遍历xaml文件中的显示内容

anauzrmj  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(144)

我是用materialDesign开源UI库开发的,对于我想使用的数据模板进行显示,代码如下,但是界面的显示风格不正确,显示逻辑也不正确

<materialDesign:Card>
<TabControl VerticalContentAlignment="Top" materialDesign:ColorZoneAssist.Mode="PrimaryMid"
    ItemsSource="{Binding IndexMenus}" Style="{StaticResource MaterialDesignNavigationRailTabControl}">
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="SelectionChanged">
            <b:InvokeCommandAction Command="{Binding NavigateCommand}"
                CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=TabControl}}" />
        </b:EventTrigger>
    </b:Interaction.Triggers>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TabItem Margin="0,25,0,0">
                <TabItem.Header>
                    <StackPanel Width="auto" Height="auto">
                        <materialDesign:PackIcon Width="24" Height="24" HorizontalAlignment="Center"
                            Kind="{Binding Icon}" />
                        <TextBlock HorizontalAlignment="Center" Text="{Binding Title}" />
                    </StackPanel>
                </TabItem.Header>
            </TabItem>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

</materialDesign:Card>
这是它现在的样子

如果未使用数据模板,他将显示此

s8vozzvw

s8vozzvw1#

您应该使用ItemTemplate而不是ContentTemplate

<TabControl.ItemTemplate>
    <DataTemplate>
        <TabItem Margin="0,25,0,0">
            <TabItem.Header>
                <StackPanel Width="auto" Height="auto">
                    <materialDesign:PackIcon Width="24" Height="24" HorizontalAlignment="Center"
                        Kind="{Binding Icon}" />
                    <TextBlock HorizontalAlignment="Center" Text="{Binding Title}" />
                </StackPanel>
            </TabItem.Header>
        </TabItem>
    </DataTemplate>
</TabControl.ItemTemplate>

相关问题