我正在尝试为多项式函数创建动态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
}
最后,我应该看到类似此样本输出的东西
<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>
系数是双重列表(系数值(。
我认为我对绑定有问题,我得到了" system.windows.markup.xamlparseexception;无法将类型的对象施放为'mseternal.internal.nemedobject'type'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>