我需要向最终用户显示一份建议列表。希望列表中的第1项最易于阅读,第2项稍小,第3项甚至更小。
- 索引为0的项目必须很大,所有文本都是黑色的
- 索引为1的项目必须略小,所有文本基本上是黑色的,但其中夹杂着一些灰色
- 索引为2或更高的项目必须很小,所有文本都带有灰色
示例
我正在使用WPF,但尚未找到实现此目标的方法。
目前我的代码如下:
<ListView ItemsSource="{Binding MyList}" Height="auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding SomeText}"/>
<Label Grid.Column="1" Content="{Binding MoreText}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
这只生成一个平面列表,其中每个元素大小相同。
我一直在研究AlternationCount + ItemsControl.AlternationIndex,但那些会交替出现,这不是我想要的,我想要一个特殊的第1行和第2行,其余行都相同。
编辑解决方案 感谢@Adrian Faciu提供的解决方案。
它看起来像这样:
<ItemsControl ItemsSource="{Binding MyList}" AlternationCount="1000" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<ItemsControl.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Red"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}" Value="0">
<Setter Property="Foreground" Value="Green"></Setter>
<Setter Property="FontSize" Value="20" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}" Value="1">
<Setter Property="Foreground" Value="Yellow"></Setter>
<Setter Property="FontSize" Value="15" />
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding MyText}"/>
<Label Grid.Column="1" Content="{Binding AnotherText}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>