WindowsPhone8 ListBox 实现手风琴折叠菜单效果

WindowsPhone8 ListBox 实现手风琴折叠菜单效果

  XAML示例代码如下:
 
    
        


        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

 
         
 

CS示例代码如下:
               //生活指数
                String[] lift = _currentApp._address.lifeInfo.Split('\n').ToArray();
                Dictionary dic = new Dictionary() { };
                if (lift != null)
                {
                    for (int i = 0, len = lift.Length; i < len; i++)
                    {
                        String[] lift2 = lift[i].Split(':').ToArray();
                        if (lift2.Length == 2 && !dic.Keys.Contains(lift2[0].Trim()))
                        {
                            dic[lift2[0].Trim()] = lift2[1].Trim();
                        }
                    }
                }
                if (dic != null)
                {
                    //清空
                    this.lboLifeData.Items.Clear();

                    int i = 0;
                    foreach (var item in dic)
                    {
                        MyLifeListBoxitem lbitem = new MyLifeListBoxitem(item.Key, item.Value, i);
                        this.lboLifeData.Items.Add(lbitem);
                        i++;
                    }
                }




 public class MyLifeListBoxitem : ListBoxItem
    {
        #region 全局变量
        private int j = new int();

        //定义文本
        private TextBlock tbk = new TextBlock();

        //定义图片
        private Image img = new Image();

        //定义边框 Border
        private Border bd = new Border();

        //定义父面板
        private StackPanel spFather = new StackPanel();

        //定义表头面板
        private StackPanel spHead = new StackPanel();

        //定义内容视图
        private ScrollViewer svContent = new ScrollViewer();
        #endregion


         /// 
        /// 构造函数
        /// 
        /// key
        /// value
        /// 
        public MyLifeListBoxitem(String key, String value, int i)
        {
            j = i;

            //填充数据
            if (!String.IsNullOrEmpty(key))
            {
                /***** 表头 *****/
                tbk = new TextBlock();
                tbk.Text = key;
                tbk.Style = (System.Windows.Style)Application.Current.Resources["MenuCssText"];

                img = new Image();
                img.Style = (System.Windows.Style)Application.Current.Resources["MenuCssImage"];
                img.ManipulationStarted += img_ManipulationStarted;

                spHead = new StackPanel();
                spHead.Orientation = Orientation.Horizontal;
                spHead.Children.Add(img);
                spHead.Children.Add(tbk);

                /***** 内容 *****/
                tbk = new TextBlock();
                tbk.Text = value;
                tbk.TextWrapping = TextWrapping.Wrap;
                tbk.Style = (System.Windows.Style)Application.Current.Resources["spTwoDaysCss_txt"];

                svContent = new ScrollViewer();
                svContent.Name = "sv_" + i;
                svContent.Style = (System.Windows.Style)Application.Current.Resources["ContentCss"];
                svContent.Content = tbk;

                /***** 父面板 *****/
                spFather = new StackPanel();
                spFather.Background = new SolidColorBrush(Colors.Orange);
                spFather.Orientation = Orientation.Vertical;
                spFather.Opacity = 0.85;
                spFather.Children.Add(spHead);
                spFather.Children.Add(svContent);

                /***** 边框 *****/
                bd = new Border();
                bd.Style = (System.Windows.Style)Application.Current.Resources["MenuCss"];
                bd.Child = spFather;

                this.Content = bd;
            }
        }


        /// 
        /// 选项卡伸展/收缩事件
        /// 
        /// 
        /// 
        public void img_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            Image img = (Image)sender;

            if (img != null)
            {
                Border bd = (Border)((StackPanel)((StackPanel)img.Parent).Parent).Parent;
                ScrollViewer sv = (ScrollViewer)bd.FindName("sv_" + j);

                if (((System.Windows.Media.Imaging.BitmapImage)(img.Source)).UriSource.ToString() == "/Images/new.png")
                {
                    img.Source = new BitmapImage(new Uri("/Images/minus.png", UriKind.Relative));
                    bd.Height += 350;
                    sv.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    img.Source = new BitmapImage(new Uri("/Images/new.png", UriKind.Relative));
                    bd.Height -= 350;
                    sv.Visibility = System.Windows.Visibility.Collapsed;
                }
            }

            e.Complete();
            e.Handled = true;
        }
    }
源码下载地址: http://pan.baidu.com/s/1je2n0

以上案例仅供学习参考,如有不足之处还请提出指正,谢谢!

效果图如下:
WindowsPhone8 ListBox 实现手风琴折叠菜单效果_第1张图片

你可能感兴趣的:(WindowsPhone8 ListBox 实现手风琴折叠菜单效果)