• 使用 ExpanderView 控件动态递归呈现内容


        public partial class ViewComments_ExpanderPage : PhoneApplicationPage
    {
    ResourceDictionary dic = Application.Current.Resources;
    CallbackCommentData ccd;
    public ViewComments_ExpanderPage()
    {
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(ViewComments_ExpanderPage_Loaded);
    }

    void ViewComments_ExpanderPage_Loaded(object sender, RoutedEventArgs e)
    {
    ccd = new CallbackCommentData();
    ccd.OpenCompleted += new EventHandler(ccd_OpenCompleted);
    ccd.Open();
    }

    void ccd_OpenCompleted(object sender, EventArgs e)
    {
    DownloadStringCompletedEventArgs even = e as DownloadStringCompletedEventArgs;
    if (even.Error != null)
    throw new Exception(even.Error.ToString());

    foreach (Comment comment in ccd.comments)
    {
    ExpanderView ev = CreateExpanderView(comment);
    stackPanel.Children.Add(ev);
    }

    }

    ExpanderView CreateExpanderView(Comment comment)
    {

    ExpanderView expander = new ExpanderView { IsExpanded = true };
    //{
    // FontSize = (double)dic["PhoneFontSizeExtraLarge"],
    // Foreground = (SolidColorBrush)dic["PhoneForegroundBrush"],
    // FontFamily = (FontFamily)dic["PhoneFontFamilySemiLight"],
    // Padding = new Thickness(0, 10, 0, 10),
    // IsExpanded = true
    //};
    //expander.HeaderTemplate.SetValue(HeaderedItemsControl.HeaderTemplateProperty,CreateDataTemplate(comment.content));
    //expander.HeaderTemplate = (DataTemplate)CreateDataTemplate(comment.content);
    expander.Header = CreateDataTemplate(comment);//comment.content;

    //if (comment != null && comment.children != null)
    // expander.Items.Add(CreateItem(comment.children));
    if (comment.children != null && comment.children.Count > 0)
    {
    foreach (var item in comment.children)
    {
    expander.Items.Add(CreateExpanderView(item));
    }
    }
    return expander;
    }

    StackPanel CreateDataTemplate(Comment comment)
    {
    //DataTemplate dataTemplate = new DataTemplate();
    StackPanel stackPanel = new StackPanel
    {
    Orientation = System.Windows.Controls.Orientation.Vertical
    };
    TextBlock textBlock = new TextBlock
    {
    Text = comment.content,
    Foreground = (SolidColorBrush)dic["PhoneForegroundBrush"],
    FontSize = 25,
    FontFamily = (FontFamily)dic["PhoneFontFamilySemiLight"],
    TextWrapping = TextWrapping.Wrap
    };

    TextBlock textBlock2 = new TextBlock
    {
    Text = comment.user.nick,
    Foreground = new SolidColorBrush(Colors.Magenta),//(SolidColorBrush)dic["PhoneForegroundBrush"],
    FontSize = 20,
    FontFamily = (FontFamily)dic["PhoneFontFamilySemiLight"],
    TextWrapping = TextWrapping.Wrap
    };
    stackPanel.Children.Add(textBlock2);
    stackPanel.Children.Add(textBlock);
    //dataTemplate.SetValue(HeaderedItemsControl.HeaderTemplateProperty, textBlock); //给 ExpanderView.TemplateProperty 赋值
    return stackPanel;
    }

    StackPanel CreateItem(List<Comment> comments)
    {
    //ItemCollection itemCollection = new ItemCollection();
    StackPanel stackPanel = new StackPanel();
    foreach (Comment item in comments)
    {
    //TextBlock textblock1 = new TextBlock
    //{
    // Text = item.content,
    // Margin = new Thickness(0, 8, 0, -4),
    // Foreground = (SolidColorBrush)dic["PhoneForegroundBrush"],
    // FontSize = (double)dic["PhoneFontSizeExtraLarge"],
    // FontFamily = (FontFamily)dic["PhoneFontFamilySemiLight"]
    //};
    //stackPanel.Children.Add(textblock1);

    TextBlock textblock2 = new TextBlock
    {
    Text = item.jid,
    TextWrapping = TextWrapping.Wrap,
    Margin = new Thickness(0, 0, 0, -2),
    Foreground = (SolidColorBrush)dic["PhoneAccentBrush"],
    FontSize = (double)dic["PhoneFontSizeNormal"],
    FontFamily = (FontFamily)dic["PhoneFontFamilyNormal"]
    };

    stackPanel.Children.Add(textblock2);
    }

    return stackPanel;
    }
    }


    重点是:  expander.Header = CreateDataTemplate(comment);//comment.content;

    Header 为 object 的类型,可以为其赋予任何类型的内容。s

  • 相关阅读:
    jquery.autocomplete.js 插件的自定义搜索规则
    经测试可用的汉字转拼音及汉字取首字母
    微信小程序UI学习
    微信小程序的生命周期和APP对象的使用
    微信小程序的配置详解
    微信小程序事件
    视图和渲染
    微信小程序<一>
    JS实战篇
    获取重复字符串的range,设置attributedText
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2425801.html
Copyright © 2020-2023  润新知