Guest User

Untitled

a guest
Jan 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <ItemsControl Name="DialogItemsControl" ItemsSource="{Binding Messages, Mode=OneWay}" Background="Transparent"
  2. BorderBrush="Transparent" TargetUpdated="DialogItemsControl_TargetUpdated">
  3. <ItemsControl.ItemTemplate><!-- For ever message -->
  4. <DataTemplate>
  5. <Grid Margin="0,0,0,20">
  6. <ItemsControl Name="SubDialogItemsControl"
  7. Foreground="{DynamicResource ButtonTextBrush}"
  8. ItemsSource="{Binding Lines,NotifyOnTargetUpdated=True}"
  9. Margin="0,0,0,12"
  10. Grid.Column="0">
  11. <ItemsControl.ItemTemplate><!-- For every line -->
  12. <DataTemplate>
  13. <TextBlock Name="DialogMessageText"
  14. Text="{Binding NotifyOnTargetUpdated=True}"
  15. VerticalAlignment="Top"
  16. Margin="0,2,0,2"
  17. TextTrimming="WordEllipsis"/>
  18. </DataTemplate>
  19. </ItemsControl.ItemTemplate>
  20. </ItemsControl>
  21. </Grid>
  22. </DataTemplate>
  23. </ItemsControl.ItemTemplate>
  24. </ItemsControl>
  25.  
  26. private void DialogItemsControl_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
  27. {
  28. ItemsControl itemControl = sender as ItemsControl;
  29.  
  30. ContentPresenter dp = itemControl.ItemContainerGenerator.ContainerFromItem(itemControl.Items.CurrentItem) as ContentPresenter;
  31.  
  32. // Finding textBlock from the DataTemplate that is set on that ContentPresenter
  33. DataTemplate myDataTemplate = dp.ContentTemplate;
  34. ItemsControl itc = (ItemsControl)myDataTemplate.FindName("SubDialogItemsControl", dp);
  35. if (itc != null && itc.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  36. {
  37. ContentPresenter cp = itc.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter;
  38. DataTemplate dt = cp.ContentTemplate;
  39. TextBlock tb = dt.LoadContent() as TextBlock;
  40.  
  41. tb.TargetUpdated += new EventHandler<System.Windows.Data.DataTransferEventArgs>(myTextBlock_TargetUpdated);
  42. }
  43. }
  44.  
  45. void myTextBlock_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
  46.  
  47. {
  48.  
  49. TextBlock tb = sender as TextBlock;
  50. //When i access the text property of tb, its showing null, how to get the text
  51. }
  52.  
  53. public class CustomTextBlock : TextBlock
  54. {
  55. static CustomTextBlock()
  56. {
  57. TextProperty.OverrideMetadata(typeof(CustomTextBlock), new FrameworkPropertyMetadata(null,
  58. new PropertyChangedCallback(
  59. (dpo, dpce) =>
  60. {
  61. //Flash the background to yellow for 2 seconds
  62. var myTxtblk = dpo as CustomTextBlock;
  63. if (myTxtblk != null)
  64. {
  65. myTxtblk.Background = Brushes.Yellow;
  66. Task.Factory.StartNew(
  67. () =>
  68. {
  69. Thread.Sleep(2000);
  70. Application.Current.Dispatcher.Invoke(
  71. new Action(() =>
  72. {
  73. myTxtblk.Background = Brushes.Transparent;
  74. }));
  75. });
  76. }
  77. })));
  78. }
  79. }
  80.  
  81. <local:CustomTextBlock Text="{Binding MyDynamicText}"/>
Add Comment
Please, Sign In to add comment