public class MessageBox : ContentControl
{
///
/// The DataTemplate to use when Message.Side == Side.ME
///
public DataTemplate MeTemplate {
get;
set; }
///
/// The DataTemplate to use when Message.Side == Side.YOU
///
public DataTemplate YouTemplate {
get;
set; }
protected override void OnContentChanged(
object oldContent,
object newContent)
{
base.OnContentChanged(oldContent, newContent);
// Apply the required template
Message message = newContent
as Message;
if (message.Side == MessageSide.ME)
{
ContentTemplate = MeTemplate;
}
else {
ContentTemplate = YouTemplate;
}
}
}
看到了,很简单吧。好了,看看效果...