我正在尝试为多项式函数创建动态UI。 由于我事先不知道订单,因此我想动态创建它。
我正在将系数列表设置为项目控制的项目源,并计划根据索引更改数据模板。
模板应该像这样
<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>
最后,我应该看到类似Sample output
的内容{{1}}
CoeffList是双精度值(系数的值)的列表。
我认为绑定有问题,出现了“ System.Windows.Markup.XamlParseException;无法将类型为MS.Internal.NamedObject的对象转换为类型为System.Windows.DataTemplate的对象” 错误
答案 0 :(得分:0)
您可以在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>