惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

博客园 - 蝗虫的大腿

IOS UITest 初始化 ViewController wp8.1 sdk preview 预览版 windows phone 网络开发三部曲(一)各种包的各种抓法 Windows phone UI虚拟化和数据虚拟化(二) Windows phone UI虚拟化和数据虚拟化(一) LongListSelector 控件 在 wp7 和wp8中的不同之处 windows phone 设计时 数据源 xna 触控操作 备忘 windows phone 在代码中生成ApplicationBar WP8多分辨率解决方案 wp8 模拟器启动失败的解决方法。 Xap 包装失败。未将对象引用设置到对象的实例。 解决办法 wp7 中Panorama控件 title 字体大小 样式的设置 不同服务器数据库之间的数据操作 jquery UI dialog 和 asp.net 控件 出错 失效 - 蝗虫的大腿 在sql server 中执行带参数的存储过程及计算运行时间 对于 抽象类 接口 的理解 http错误列表 常用的js正则表达式 - 蝗虫的大腿 - 博客园
wp8 longlistselector 动态加载datatemplate
蝗虫的大腿 · 2013-12-25 · via 博客园 - 蝗虫的大腿

在做一个windows phone 8 即时通讯应用的时候,聊天界面的对话气泡。 需要根据不同的消息类型,加载对应的DataTemplate,
比如发送,接受,图片,语音,等气泡。 如下图所示

会话界面

主要思想就是,定义多个不同的模板,在每一项的内容生成的时候,根据数据源的类型,加载对应的模板。

看代码

 1 public abstract class DataTemplateSelector:ContentControl
 2     {
 3             //根据newContent的属性,返回所需的DataTemplate
 4             public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
 5             {
 6                 return null;
 7             }
 8 
 9             protected override void OnContentChanged(object oldContent, object newContent)
10             {
11                 base.OnContentChanged(oldContent, newContent);
12                 //根据newContent的属性,选择对应的DataTemplate
13                 ContentTemplate = SelectTemplate(newContent, this);
14             }
15 
16     }
17     public class ChatTemplateSelector : DataTemplateSelector
18     {
19         public DataTemplate ReciveMsgTP { get; set; }
20         public DataTemplate SendMsgTP { get; set; }
21 
22         //根据newContent的属性,返回所需的DataTemplate
23         public override DataTemplate SelectTemplate(object item, DependencyObject container)
24         {
25             ChatModel model = item as ChatModel;
26             DataTemplate dt;
27             if (model != null)
28             {
29                 switch (model.CMType)
30                 {
31                     case MessageType.ReciveMsg:
32                         dt= ReciveMsgTP;
33                         break;
34                     case MessageType.SendMsg:
35                         dt= SendMsgTP;
36                         break;
37                     default:
38                         dt = ReciveMsgTP; 
39                         break;
40                 }
41                 return dt;
42             }
43             return base.SelectTemplate(item, container);
44         }
45     }

.xaml

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="ItemTP">
            <local:ChatTemplateSelector  Content="{Binding}">
                <local:ChatTemplateSelector.ReciveMsgTP>
                    <DataTemplate>
                        <Grid d:DesignWidth="415.522" d:DesignHeight="106.567" Height="113" Width="501">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="124*"/>
                                <ColumnDefinition Width="43*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Msg}" VerticalAlignment="Top" FontSize="30" Height="0" Width="0"/>
                            <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="100" Margin="10,3,0,0" VerticalAlignment="Top" Width="315" Background="#FFF56D6D"/>
                        </Grid>
                    </DataTemplate>

                </local:ChatTemplateSelector.ReciveMsgTP>
                <local:ChatTemplateSelector.SendMsgTP>
                    <DataTemplate>
                        <Grid d:DesignWidth="415.522" d:DesignHeight="106.567" Height="128" Width="498">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="59*"/>
                                <ColumnDefinition Width="190*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Msg}" VerticalAlignment="Top" FontSize="30" Height="0" Width="0"/>
                            <Border BorderBrush="Black" BorderThickness="1" Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="10,15,0,0" VerticalAlignment="Top" Width="348" Background="#FF0F7DEC"/>
                        </Grid>
                    </DataTemplate>
                </local:ChatTemplateSelector.SendMsgTP>
            </local:ChatTemplateSelector>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>