问题
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.
The template should work like this
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 is a list of doubles, (values of coeffients).
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
回答1:
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>
来源:https://stackoverflow.com/questions/56055748/how-to-change-the-data-template-in-itemscontrol-based-on-index