具有删除大小的WPF列表

具有删除大小的WPF列表,wpf,list,Wpf,List,我必须向最终用户显示一个建议列表。 我想要1。列表中最容易阅读的项目是2。项目应该是小一点和3。项目甚至更小 索引0处的项必须是大的,并且所有文本都是黑色的 索引1中的项目必须稍微小一些,所有文本主要是黑色的,但有一些灰色 索引2和更高的项目必须是小的,所有文本都是灰色的 示例 我正在使用WPF,但还没有找到一种方法来做到这一点 目前我有: <ListView ItemsSource="{Binding MyList}" Height="auto" ScrollViewer.Vert

我必须向最终用户显示一个建议列表。 我想要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>

这只会在每个元素大小相同时生成一个平面列表

我一直在看,但这些会交替,这不是我想要的,我想要一个特别的1。二,。行,其余行相同

编辑解决方案 感谢@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>


我认为您可以向类中添加属性,并将其与标签的字体大小绑定

Xaml:


视图模型:

  MyList = new List<ListItems>();
        MyList.Add(new ListItems() { Name = "Option A", FontSize = 20, Foreground = Brushes.Black });
        MyList.Add(new ListItems() { Name = "Option B", FontSize = 15, Foreground = Brushes.Black });
        MyList.Add(new ListItems() { Name = "Option C", FontSize = 8, Foreground = Brushes.Gray });
        MyList.Add(new ListItems() { Name = "Option D", FontSize = 8, Foreground = Brushes.Gray });
        MyList.Add(new ListItems() { Name = "Option E", FontSize = 8, Foreground = Brushes.Gray });
MyList=newlist();
添加(新的ListItems(){Name=“Option A”,FontSize=20,前台=Brusks.Black});
添加(新的ListItems(){Name=“Option B”,FontSize=15,前台=brusks.Black});
添加(新的ListItems(){Name=“Option C”,FontSize=8,前台=Brusks.Gray});
添加(新的ListItems(){Name=“Option D”,FontSize=8,前台=brusks.Gray});
添加(新的ListItems(){Name=“Option E”,FontSize=8,前台=Brusks.Gray});

你走对了方向。您可以将AlternationCount绑定到集合的长度,然后为默认项创建样式,并更改第一行的样式:

<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工作,需要更改绑定:

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


有关更多信息,请参阅。

我无法使其正常工作。它们都是绿色的(就像它们都是AlternationIndex=0)。我已将Listview设置为AlternationCount=“1000”看起来AlternationIndex和Listview存在问题。请参见此处:使用链接,并使用ItemsController而不是listview成功了!:)
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="1">
     <Setter Property="Foreground" Value="Yellow"></Setter>
</DataTrigger>