[WP7]实现类似tree效果

#目的

实现tree效果。


#前提条件

需要区分WP7.0和WP7.1+版本。

对于WP7.0版本,可采用Listbox嵌套listbox方法实现;

对于WP7.1+(Mango)版本,则多一项选择,采用系统的toolkit方法,即ExpanderView实现。


#实现过程

本文目前采用listbox嵌套方法实现。(demo,有bug。。。)

1.无需声明命名空间。

2.demo.xaml中使用如下代码:

<!--ContentPanel - tree with item and sub item-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="4,4,4,4">
            <ListBox x:Name="MainList">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel >
                            <TextBlock Text="{Binding Name}" MouseLeftButtonDown="Text_MouseLeftButtonDown" FontSize="40"></TextBlock>
                            <ListBox Visibility="Collapsed" ItemsSource="{Binding Chapters}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Margin="5">
                                            <TextBlock Text="{Binding Title}" FontSize="30">
                                            </TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
3.在demo.xaml.cs中使用如下代码:

声明控件使用:

using System.Collections.ObjectModel;
代码实现:

  public treeListPage()
        {
             InitializeComponent();          
            ObservableCollection<Book> books = new ObservableCollection<Book>();
             for (int i = 0; i < 10; i++)
             {
                 Book book = new Book { Name = "Book" + i.ToString()};
                 book.Chapters = new List<Chapter>();
                 for (int j = 0; j < 10; j++)
                     book.Chapters.Add(new Chapter { Title = "Chapter" + i.ToString() + "_" + j.ToString()});
                 books.Add(book);
             }
             this.MainList.ItemsSource = books;
        }

        public class Book
        {
            public string Name { get; set; }
            public List<Chapter> Chapters { get; set; }
        }
 
        public class Chapter
        {       
             public string Title{get;set;}                
            public string Page{get;set;}        
        } 

        private void Text_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                TextBlock t = sender as TextBlock;
                StackPanel p = VisualTreeHelper.GetParent(t) as StackPanel;
                ListBox sublist = p.Children[1] as ListBox;
                if (sublist.Visibility == Visibility.Collapsed)
                    sublist.Visibility = Visibility.Visible;
                else
                    sublist.Visibility = Visibility.Collapsed;
            }   

        private void Text_MouseLeftButtonDown2(object sender, MouseButtonEventArgs e)
        {

        }

#效果


#遗留问题

1.目前在Chapter*上点住时,无法响应拖动事件。

2.打开Book目录时,有时不能将当前打开的作为展现页面呈现出来。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值