我在listbox datatemplate中有2个文本块。 我想按字母顺序显示名称列表。第一个文本块应仅在具有该字符的字符串的开头显示名称的第一个字符,第二个文本块应显示该字符串。
请查看以下链接
http://i.stack.imgur.com/4mdtu.png
就像:
A Ana
Andrew
Andy
B Bane
Bob
C Chris
Christian
Catherine
我试过这个: XAML:
<ListBox Height="331" Name="lstBoxPlayers" MinHeight="200" MinWidth="150" Margin="0,0,0,20" SelectionChanged="lstBoxPlayers_SelectionChanged"
Tap="lstBoxPlayers_Tap" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="0,0,0,1" MinWidth="200" Margin="0,0,0,0" Grid.Column="0">
<Grid Background="White" MinWidth="200" Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="Abrev" Text="{Binding Path=PlayerShortName[0]}" FontSize="30" Foreground="Black" Height="Auto" Width="26" TextAlignment="Center">
</TextBlock>
<TextBlock Grid.Column="1" Text="{Binding PlayerShortName}" Foreground="Black" FontSize="16" Height="29" MaxWidth="160" Margin="-25,0,0,0" TextAlignment="Center">
</TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
代码背后的代码:
lstBoxPlayers.ItemsSource = PlayerList; //List of string
答案 0 :(得分:0)
您可以使用GroupBy形成字母组,然后将生成的组集合绑定到ListBox ..
背后的代码如下:
lstBoxPlayers.ItemsSource = PlayerList.GroupBy( p => p.PlayerShortName[ 0 ] );
你网格中的Xaml看起来像:
<!--Key of the Group-->
<TextBlock x:Name="Abrev" Text="{Binding Path=Key}" FontSize="30" Foreground="Black" Height="Auto" Width="26" TextAlignment="Center" VerticalAlignment="Top"></TextBlock>
<!--Values of the Group-->
<ItemsControl Grid.Column="1" ItemsSource="{Binding}" Margin="-25,0,0,0" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Column="1" Text="{Binding PlayerShortName}" Foreground="Black" FontSize="16" Height="29" MaxWidth="160" TextAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>