gpt4 book ai didi

具有降序大小的 WPF 列表

转载 作者:行者123 更新时间:2023-12-04 18:24:32 25 4
gpt4 key购买 nike

我必须向最终用户显示建议列表。我希望列表中的 1.item 最容易阅读,2.item 应该小一点,3.item 更小。

  • 索引 0 处的项目必须很大并且所有文本都是黑色
  • 索引 1 的项目必须小一点,所有文本主要是黑色,但有一些灰色
  • 索引 2 及更高位置的项目必须较小且所有文本呈灰色

示例

Like this

我正在使用 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>

最佳答案

您走在正确的轨道上。您可以将 AlternationCount 绑定(bind)到集合的长度,然后为默认项目创建样式,并首先更改行:

<Style x:Key="differentItemsStyle" 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>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}" Value="1">
<Setter Property="Foreground" Value="Yellow"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>

在您的示例中,选项 C、D、E 具有默认样式,您可以根据需要为选项 A 和选项 B 覆盖它。

编辑为了使 ListBox 工作,需要更改绑定(bind):

<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="1">
<Setter Property="Foreground" Value="Yellow"></Setter>
</DataTrigger>

参见 this answer了解更多信息。

关于具有降序大小的 WPF 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com