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

本文深入探讨了在XAML中创建自定义控件的实现过程,包括如何利用CallbackCommentData类和事件处理器来实现实时数据加载与展示。详细介绍了通过资源字典、模板和数据绑定等技术构建复杂UI元素的方法,旨在提高应用的可复用性和用户体验。
摘要由CSDN通过智能技术生成
    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/archive/2012/03/30/2425801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值