如何在基于索引的itemscontrol中更改数据模板? [英] How to change the data template in itemscontrol based on index?

查看:104
本文介绍了如何在基于索引的itemscontrol中更改数据模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a dynamic UI for polynomial function. Since I do not know the order beforehand, I want to create it dynamically.

我将系数列表设置为项目控制和计划中的项目源

I am setting my coefficient list as a items source of items control and planned to change data template based on the index.

模板应按以下方式工作

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
}

最后,我应该会看到类似示例输出

In the end, I should see something like this 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是一个双精度列表,(系数的值)。

CoeffList is a list of doubles, (values of coeffients).

我认为绑定有问题,出现了 System.Windows.Markup.XamlParseException;无法转换类型为 MS.Internal.NamedObject的对象进行输入'System.Windows.DataTemplate
错误

I think I have a problem with bindings, I got " System.Windows.Markup.XamlParseException; Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.DataTemplate" error

推荐答案

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

You may use ordinary Triggers and a default ContentTemplate in an ItemContainerStyle:

<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>

这篇关于如何在基于索引的itemscontrol中更改数据模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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