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

推荐订阅源

P
Palo Alto Networks Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
GbyAI
GbyAI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
Recent Announcements
Recent Announcements
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
NISL@THU
NISL@THU
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
F
Full Disclosure
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
D
Docker
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
Help Net Security
Help Net Security
V
Visual Studio Blog
小众软件
小众软件
B
Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 蝗虫的大腿

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>