[英]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. 我有一个由
SortDescription
和GroupDescription
排序和分组的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 这是给你的样品
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 然后是列表框
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.