I have a RichTextBox control inside a DataTemplate for a ListView. The idea is that I want to dynamically add Runs/InlineUIElements/images, etc to the rich text boxes in the Listview at Bind time. The problem is there's no ondatabinding or similar event. I tried the Loaded event of the RichTextBox but it appears that WPF reuses controls so the content was being messed up when I scrolled (putting the wrong content in the wrong order, because the load events were firing during scroll). I should also mention the binding to the ListView is taking place in the codebehind, by manually adding rows to the ListView.Items collection.
Relevant markup
<ListView Background="#F7F7F7" HorizontalAlignment="Stretch" Foreground="Black" x:Name="chatPane" Grid.Row="1"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" SelectionMode="Multiple"
ItemTemplateSelector="{StaticResource messageTypeDataTemplateSelector}" SelectionChanged="ChatPane_OnSelectionChanged">
</ListView>
<common:MessageTypeDataTemplateSelector
TextMessageTemplate="{StaticResource TextMessage}"
EnterMessageTemplate="{StaticResource EnterMessage}"
ExitMessageTemplate="{StaticResource ExitMessage}"
TimestampMessageTemplate="{StaticResource TimestampMessage}"
ImageMessageTemplate="{StaticResource ImageMessage}"
x:Key="messageTypeDataTemplateSelector" />
<DataTemplate x:Key="TextMessage">
<Grid Grid.ColumnSpan="3" RowSpan="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width=".5*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding UserName}" Visibility="{Binding Source={StaticResource chatSettings}, Path=HideAvatars, Converter={StaticResource booleanToVisibility}}" FontWeight="Bold" TextAlignment="Right" Grid.Column="0" Width="150" />
<Image VerticalAlignment="Top" Source="{Binding AvatarUrl}" Visibility="{Binding Source={StaticResource chatSettings}, Path=ShowAvatars, Converter={StaticResource booleanToVisibility}}" Grid.Column="0" Width="60" Margin="0,0,10,0" />
<TextBlock Text=" : " Visibility="{Binding Source={StaticResource chatSettings}, Path=HideAvatars, Converter={StaticResource booleanToVisibility}}" Grid.Column="1" />
<RichTextBlock Loaded="FrameworkElement_OnLoaded" TextWrapping="Wrap" Grid.Column="2" />
</Grid>
</DataTemplate>