如何根据索引更改itemscontrol中的数据模板?

How to change the data template in itemscontrol based on index?

我正在尝试为多项式函数创建动态 UI。 由于我事先不知道顺序,所以我想动态创建它。

我正在将我的系数列表设置为项目控制的项目源,并计划根据索引更改数据模板。

模板应该像这样工作

if (index > 1)
{
  (coefficient text box) + *x ^ (index) + //Polynomial2
}
 else if (index == 1)
 {
  (coefficient text box) + *x +  //Polynomial 1
 }
 else if (index == 0)
{
  (coefficient text box)  //Polynomial
}

最后应该是这样的Sample output

 <DataTemplate x:Key="PolynomialEquationTemplate" >

        <StackPanel Orientation="Horizontal">
            <Label Content="y=" Width="50"></Label>
            <ItemsControl ItemsSource="{Binding DataContext.CoeffList, RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type Window}}}" AlternationCount="100" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ContentControl Content={Binding .}>
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), 
                RelativeSource={RelativeSource TemplatedParent} }" Value="1">
                                            <Setter Property="ContentTemplate"
                                    Value="{StaticResource Polynomial1}" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), 
                RelativeSource={RelativeSource TemplatedParent}}" Value="0">
                                            <Setter Property="ContentTemplate"
                                    Value="{StaticResource Polynomial0}" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), 
                RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibility}}" Value="True">
                                            <Setter Property="ContentTemplate"
                                    Value="{StaticResource Polynomial2}" />
                                        </DataTrigger>

                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>

        </StackPanel>

    </DataTemplate>


    <DataTemplate x:Key="Polynomial2" >
        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding ., RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type ItemsControl}}}"></TextBox>
            <Label Content=")*x^"></Label>
            <Label Content="{Binding Path=(ItemsControl.AlternationIndex), 
                RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type ItemsControl}}}"></Label>
            <Label Content="+"></Label>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="Polynomial0" >
        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding .,  RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type ItemsControl}}}"></TextBox>
          </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="Polynomial1" >
        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding ., RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type ItemsControl}}}"></TextBox>
            <Label Content=")*x"></Label>
            <Label Content="+"></Label>
        </StackPanel>
    </DataTemplate>

CoeffList 是双精度列表(系数值)。

我认为我的绑定有问题,我得到“System.Windows.Markup.XamlParseException;无法将类型 'MS.Internal.NamedObject' 的对象转换为类型‘System.Windows.DataTemplate” 错误

您可以在 ItemContainerStyle 中使用普通触发器和默认 ContentTemplate:

<ItemsControl AlternationCount="100" ItemsSource="{Binding ...}">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">

            <!-- default for any index >= 2 -->
            <Setter Property="ContentTemplate"
                    Value="{StaticResource Polynomial2}"/>

            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource Polynomial0}"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource Polynomial1}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>