ItemsControl中的不同控件

时间:2019-07-08 06:10:40

标签: wpf xaml

我有一个这样定义的ItemsControl:

<ItemsControl ItemsSource="{Binding PageViewModels}">
    <ItemsControl.Style>
        <Style TargetType="ItemsControl">
            <Style.Resources>
                <Style TargetType="Button" x:Key="Big">
                     <Setter Property="Content" Value="BigStyle"/>
                </Style>
            <Style TargetType="Button" x:Key="Small">
            <Setter Property="Content" Value="SmallStyle"/>
            </Style>
        </Style.Resources>
        <Setter Property="Tag" Value="{StaticResource RoundCornerButtonMagentaFilledBig}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding PageViewModels.Count}" Value="2">
                    <Setter Property="Tag" Value="{StaticResource RoundCornerButtonMagentaFilledSmall}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding CurrentPageViewModel.Name}" Value="Home Page">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.Style>
    <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <UniformGrid Columns="{Binding PageViewModels.Count}" Margin="0,19"/>
    </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button x:Name="NavigationButton"
                                Content="{Binding Name}"
                                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                CommandParameter="{Binding }"
                                Style="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}"
                                />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
</ItemsControl>

这会根据参数显示一个按钮网格。但是,我现在希望它在此网格中显示不同的控件。例如,如果PageViewModesl.Count为2,我想有一个网格

按钮按钮

如果是3:

按钮按钮

但是,如果是4岁,我想拥有:

按钮文本按钮文本

如何在其中放置不同的控件?

0 个答案:

没有答案