2

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

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

这可能吗?

更新

所以,我尝试:

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

它有效,但我仍在等待更多漂亮的建议。谢谢!:)

编辑

我的长列表选择器:

<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;
4

1 回答 1

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 的网格,如果您的项目数 < 10,则将 GroupHeaderTemplate 交换到该数据模板。

于 2013-08-01T06:21:17.610 回答