Я пытаюсь создать динамический интерфейс для полиномиальной функции.
Поскольку я не знаю заказ заранее, я хочу создать его динамически.
Я устанавливаю свой список коэффициентов в качестве источника элементов управления и планирую изменить шаблон данных на основе индекса.
Шаблон должен работать так:
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>
CoeffList представляет собой список значений типа double (значения коэффициентов).
Я думаю, что у меня есть проблема с привязками, я получил "System.Windows.Markup.XamlParseException; Невозможно привести объект типа 'MS.Internal.NamedObject' к типу 'System.Windows.DataTemplate"
Ошибка