使用 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

原文地址:https://www.cnblogs.com/hebeiDGL/p/2425801.html