简体   繁体   English

如何获取组中的第一个ListBoxItem

[英]How to get first ListBoxItem in the group

I have a ListBox sorted and grouped by SortDescription and GroupDescription and now I want to know whether there is a way to know which item is the first one in each group. 我有一个由SortDescriptionGroupDescription排序和分组的ListBox,现在我想知道是否有一种方法可以知道哪个项目是每个组中的第一个项目。 For example if I have a list of names I want to get the first Item in the list which starts with A, B, C, etc and change its template. 例如,如果我有一个名称列表,我想获取列表中以A,B,C等开头的第一项并更改其模板。

To be more clear I should say I want to change the DataTemplate of the first ListBoxItem in each group. 更清楚地说,我应该说我想更改每个组中第一个ListBoxItem的DataTemplate。 How is it possible? 这怎么可能?

Edit 编辑

This is my sample CollectionView 这是我的示例CollectionView

<CollectionViewSource
x:Key="Myiew"
Source="{Binding Items}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="FirstName" Converter="{StaticResource StringToFirstLetter}" />
    </CollectionViewSource.GroupDescriptions>
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="FirstName" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<Listbox ItemsSource={"StaticResources Myiew"} />

As I said everything works fine. 正如我所说,一切正常。 the Items are sorted and if I change the GroupStyle I see that the items are grouped, but I don't want to set a group style. 项目已排序,如果更改GroupStyle,我会看到项目已分组,但我不想设置组样式。 I just want to change the DataTemplate of the first item in each group. 我只想更改每个组中第一项的DataTemplate。

Here is a sample for you 这是给你的样品

  • create a converter class 创建一个转换器类

this will just see if the value passed is null or not 这只会查看传递的值是否为null

class FirstItemDetector : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       return value == null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

then comes the declaration of converter where l: is the namespace to your converter class 然后是转换器的声明,其中l:是转换器类的命名空间

<l:FirstItemDetector x:Key="FirstItemDetactor" />

then comes the listbox 然后是列表框

  • I added a group style (you can style however you like) solution is group independent 我添加了组样式(您可以随意设置样式)解决方案与组无关
  • added a data template as ItemTemplate of the listbox 添加了一个数据模板作为列表框的ItemTemplate
  • added a DataTrigger on PreviousData with converter as FirstItemDetactor declared as above 如上声明的FirstItemDetactor,在上一个数据上添加了DataTrigger并将其作为转换器
  • in the setter of trigger I changed the foreground to red ( you have many choice including changing styles, show hide elements etc. 在触发器的设置器中,我将前景更改为红色(您有很多选择,包括更改样式,显示隐藏元素等。

here is the listbox 这是列表框

    <ListBox ItemsSource="{Binding Source={StaticResource Myiew}}">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto" />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Border BorderBrush="Black"
                                                BorderThickness=".5"
                                                Padding="4">
                                            <TextBlock Text="{Binding Name}"
                                                       HorizontalAlignment="Center" />
                                        </Border>
                                        <ItemsPresenter Grid.Row="1" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" x:Name="text" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData},Converter={StaticResource FirstItemDetactor}}"
                                 Value="True">
                        <Setter TargetName="text"
                                Property="Foreground"
                                Value="Red" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

main role is played by the datatigger and the converter, it will work independently, with groups or even nested groups. datatigger和转换器将扮演主要角色,它将与组甚至嵌套组独立工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM