/MahApps.Metro.Controls/RevealImage.xaml.cs

https://bitbucket.org/aeoth/mahapps.metro/ · C# · 71 lines · 59 code · 12 blank · 0 comment · 0 complexity · 299652fad77a360b9609c1279cb1cd8e MD5 · raw file

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Animation;
  7. namespace MahApps.Metro.Controls
  8. {
  9. public partial class RevealImage
  10. {
  11. public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(RevealImage), new UIPropertyMetadata(""));
  12. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(RevealImage), new UIPropertyMetadata(null));
  13. public string Text
  14. {
  15. get { return (string)GetValue(TextProperty); }
  16. set { SetValue(TextProperty, value); }
  17. }
  18. public ImageSource Image
  19. {
  20. get { return (ImageSource)GetValue(ImageProperty); }
  21. set { SetValue(ImageProperty, value); }
  22. }
  23. public RevealImage()
  24. {
  25. InitializeComponent();
  26. }
  27. private static void TypewriteTextblock(string textToAnimate, TextBlock txt, TimeSpan timeSpan)
  28. {
  29. var story = new Storyboard
  30. {
  31. FillBehavior = FillBehavior.HoldEnd
  32. };
  33. DiscreteStringKeyFrame discreteStringKeyFrame;
  34. var stringAnimationUsingKeyFrames = new StringAnimationUsingKeyFrames
  35. {
  36. Duration = new Duration(timeSpan)
  37. };
  38. var tmp = string.Empty;
  39. foreach (var c in textToAnimate)
  40. {
  41. discreteStringKeyFrame = new DiscreteStringKeyFrame
  42. {
  43. KeyTime = KeyTime.Paced
  44. };
  45. tmp += c;
  46. discreteStringKeyFrame.Value = tmp;
  47. stringAnimationUsingKeyFrames.KeyFrames.Add(discreteStringKeyFrame);
  48. }
  49. Storyboard.SetTargetName(stringAnimationUsingKeyFrames, txt.Name);
  50. Storyboard.SetTargetProperty(stringAnimationUsingKeyFrames, new PropertyPath(TextBlock.TextProperty));
  51. story.Children.Add(stringAnimationUsingKeyFrames);
  52. story.Begin(txt);
  53. }
  54. private void GridMouseEnter(object sender, MouseEventArgs e)
  55. {
  56. TypewriteTextblock(Text.ToUpper(), textBlock, TimeSpan.FromSeconds(.25));
  57. }
  58. }
  59. }