如何在LongListSelector中隐藏组头?

时间:2013-07-31 13:30:53

标签: windows-phone-7 xaml windows-phone-8 longlistselector

如果列表中的项目数量很少,我想在LongListSelector中隐藏组标题(如WP中的应用列表,如果应用小于30)。

 LongListSelector.ItemsSource = myList; 
 if (myList.Count < 10) 
 {
    //hide group header
 }

这可能吗?

更新

所以,我试试:

LongListSelector.ItemsSource = myList; 
     if (myList.Count < 10) 
     {
        LongListSelector.GroupHeaderTemplate = null;
     }

它有效,但我仍然等待更多美丽的建议。谢谢! :)

修改

我的LongListSelector:

<phone:LongListSelector 
                          Name="UsersListGroup"
                          IsGroupingEnabled="True" 
                          LayoutMode="List"
                          HideEmptyGroups="False"      
                          ItemTemplate="{StaticResource usersItemTemplate}"
                          GroupHeaderTemplate="{StaticResource groupHeaderTemplate}"
                          JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"                          
                          Margin="1,36,-1,10"
                          VirtualizingStackPanel.VirtualizationMode="Recycling"/>

模板:

                                        

<DataTemplate x:Key="usersItemTemplate">
    <Grid x:Name="FriendsPanel" Grid.Row="1" Margin="12,0,12,0">            
        <StackPanel Name="FriendPanel" Orientation="Horizontal" Margin="0,0,0,17" Tag="{Binding Id}" Tap="FriendPanel_Tap" >
            <Image Height="75" 
                   Width="75" 
                   HorizontalAlignment="Left" 
                   Margin="6,0,9,0" 
                   Name="Avatar" 
                   Stretch="Fill" 
                   VerticalAlignment="Top" 
                   Source="{Binding Avatar, StringFormat=http://myurl.com/\{0\}}"/>
            <TextBlock Text="offline" 
                       TextWrapping="Wrap"
                       Margin="-75,78,0,0" 
                       Style="{StaticResource PhoneTextSmallStyle}" >
                 <i:Interaction.Triggers>     
                         <ec:DataTrigger Binding="{Binding IsOnline}" Value="1">
                              <ec:ChangePropertyAction PropertyName="Text" Value="online"/>
                         </ec:DataTrigger>
                 </i:Interaction.Triggers>
            </TextBlock>
            <StackPanel >
                <TextBlock
                           Text="{Binding Name}" 
                           TextWrapping="Wrap" 
                           Style="{StaticResource PhoneTextExtraLargeStyle}">
                </TextBlock>
                <TextBlock Text="{Binding Aboutself}" 
                           TextWrapping="Wrap" 
                           Margin="12,-6,12,0" 
                           Style="{StaticResource PhoneTextSubtleStyle}"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</DataTemplate>

<Style x:Key="LongListSelectorJumpListStyle" TargetType="phone:LongListSelector">
    <Setter Property="GridCellSize"  Value="113,113"/>
    <Setter Property="LayoutMode" Value="Grid" />
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Background="Purple" 
                        Width="113" Height="113" Margin="6" >
                    <TextBlock Text="{Binding Title}"
                               FontFamily="{StaticResource PhoneFontFamilySemiBold}"  
                               FontSize="48" Padding="6" 
                               Foreground="White"
                               VerticalAlignment="Center"/>
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

CS:

    private static ObservableCollection<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
            {
                IEnumerable<Group<T>> groupList = from item in itemList
                                                  group item by getKeyFunc(item) into g
                                                  orderby g.Key
                                                  select new Group<T>(Convert.ToString(g.Key.ToLower()[0]), g);

                var groupObservable = new ObservableCollection<Group<T>>(groupList);
                return groupObservable;
            }

   private ObservableCollection<Group<GYUser>> GetUserGroups(ObservableCollection<GYUser> collectionFriends)
            {                                       
                return GetItemGroups(collectionFriends, c => Convert.ToString(c.Name.ToLower()[0]));
            }


Friends = GetUserGroups(new ObservableCollection<GYUser>(response.Result.Friends));
UsersListGroup.ItemsSource = Friends;

1 个答案:

答案 0 :(得分:2)

您使用的是WP8中内置的LongListSelector吗?如果是这样,请尝试将IsGroupingEnabled设置为false。

http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.longlistselector.isgroupingenabled%28v=vs.105%29.aspx

如果您正在使用控件中的WP7版本,请尝试在页面资源中定义两个数据模板,其中一个具有宽度和高度为0的网格,如果没有项目,则将GroupHeaderTemplate交换到该数据模板; 10。