Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ItemsControl Name="DialogItemsControl" ItemsSource="{Binding Messages, Mode=OneWay}" Background="Transparent"
- BorderBrush="Transparent" TargetUpdated="DialogItemsControl_TargetUpdated">
- <ItemsControl.ItemTemplate><!-- For ever message -->
- <DataTemplate>
- <Grid Margin="0,0,0,20">
- <ItemsControl Name="SubDialogItemsControl"
- Foreground="{DynamicResource ButtonTextBrush}"
- ItemsSource="{Binding Lines,NotifyOnTargetUpdated=True}"
- Margin="0,0,0,12"
- Grid.Column="0">
- <ItemsControl.ItemTemplate><!-- For every line -->
- <DataTemplate>
- <TextBlock Name="DialogMessageText"
- Text="{Binding NotifyOnTargetUpdated=True}"
- VerticalAlignment="Top"
- Margin="0,2,0,2"
- TextTrimming="WordEllipsis"/>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- </Grid>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- private void DialogItemsControl_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
- {
- ItemsControl itemControl = sender as ItemsControl;
- ContentPresenter dp = itemControl.ItemContainerGenerator.ContainerFromItem(itemControl.Items.CurrentItem) as ContentPresenter;
- // Finding textBlock from the DataTemplate that is set on that ContentPresenter
- DataTemplate myDataTemplate = dp.ContentTemplate;
- ItemsControl itc = (ItemsControl)myDataTemplate.FindName("SubDialogItemsControl", dp);
- if (itc != null && itc.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
- {
- ContentPresenter cp = itc.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter;
- DataTemplate dt = cp.ContentTemplate;
- TextBlock tb = dt.LoadContent() as TextBlock;
- tb.TargetUpdated += new EventHandler<System.Windows.Data.DataTransferEventArgs>(myTextBlock_TargetUpdated);
- }
- }
- void myTextBlock_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
- {
- TextBlock tb = sender as TextBlock;
- //When i access the text property of tb, its showing null, how to get the text
- }
- public class CustomTextBlock : TextBlock
- {
- static CustomTextBlock()
- {
- TextProperty.OverrideMetadata(typeof(CustomTextBlock), new FrameworkPropertyMetadata(null,
- new PropertyChangedCallback(
- (dpo, dpce) =>
- {
- //Flash the background to yellow for 2 seconds
- var myTxtblk = dpo as CustomTextBlock;
- if (myTxtblk != null)
- {
- myTxtblk.Background = Brushes.Yellow;
- Task.Factory.StartNew(
- () =>
- {
- Thread.Sleep(2000);
- Application.Current.Dispatcher.Invoke(
- new Action(() =>
- {
- myTxtblk.Background = Brushes.Transparent;
- }));
- });
- }
- })));
- }
- }
- <local:CustomTextBlock Text="{Binding MyDynamicText}"/>
Add Comment
Please, Sign In to add comment