如何打开在WP7中折叠组的LongListSelector?

时间:2013-06-18 09:47:13

标签: windows-phone-7 longlistselector

我有一个显示大量项目的LongListSelector。当打开longListSelector时,我看到扩展的组,即项目在组内显示。我希望longList Selector显示折叠的Panel,在开始时只显示组名。像一个索引。点按某个组后,其项目会展开。怎么办呢?

1 个答案:

答案 0 :(得分:1)

我自己需要实现这一点 - 按照以下方式实现:

在XAML中的item(not header!)模板定义中,绑定包含项的Visibility属性(在我的例子中,为Grid):

<DataTemplate x:Key="itemTemplate">
  <Grid Visibility="{Binding FolderVisibility}">
  ...

ObservableCollection派生项目组,并创建一个合适的属性来处理展开/折叠状态:

public class MyGroup : ObservableCollection<MyItem>
{
    ...
    private bool m_expanded = true;
    public bool Expanded
    {
        get { return m_expanded; }
        set
        {
            m_expanded = value;
            OnPropertyChanged( new PropertyChangedEventArgs( "Expanded" ));
            foreach( var i in this )
            {
                i.OnFolderCollapsedExpanded();
            }
        }
    }
    ...

最后,您需要在每个列表项上使用FolderVisibility属性:

public class MyItem : INotifyPropertyChanged
{
    ...
    public event PropertyChangedEventHandler PropertyChanged;
    ...
    public Visibility FolderVisibility
    {
        get { return MyFolder.Expanded ? Visibility.Visible : Visibility.Collapsed; }
    }
    public void OnFolderCollapsedExpanded()
    {
        var h = PropertyChanged;
        if( h != null ) h( this, new PropertyChangedEventArgs( "FolderVisibility" ));
    }
    ...

然后只需将文件夹的Expanded属性切换到合适的位置(例如文件夹标题模板的Click处理程序)。

相关问题