DescriptionControl.xaml.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace WpfDemo
  7. {
  8. public partial class DescriptionControl : UserControl
  9. {
  10. public string Header
  11. {
  12. get { return (string)GetValue(HeaderProperty); }
  13. set { SetValue(HeaderProperty, value); }
  14. }
  15. public static readonly DependencyProperty HeaderProperty =
  16. DependencyProperty.Register("Header", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
  17. public string Description
  18. {
  19. get { return (string)GetValue(DescriptionProperty); }
  20. set { SetValue(DescriptionProperty, value); }
  21. }
  22. public static readonly DependencyProperty DescriptionProperty =
  23. DependencyProperty.Register("Description", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
  24. public string Link
  25. {
  26. get { return (string)GetValue(LinkProperty); }
  27. set { SetValue(LinkProperty, value); }
  28. }
  29. public static readonly DependencyProperty LinkProperty =
  30. DependencyProperty.Register("Link", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
  31. public string LinkText
  32. {
  33. get { return (string)GetValue(LinkTextProperty); }
  34. set { SetValue(LinkTextProperty, value); }
  35. }
  36. public static readonly DependencyProperty LinkTextProperty =
  37. DependencyProperty.Register("LinkText", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
  38. public DescriptionControl()
  39. {
  40. InitializeComponent();
  41. }
  42. public void SetDescription(string value)
  43. {
  44. var lines = value.Replace("\r\n", "\n").Split('\n').ToList();
  45. // check the last line for url
  46. if (lines.Count > 0)
  47. {
  48. var lastLine = lines[lines.Count - 1];
  49. int urlIndex = lastLine.IndexOf("http");
  50. if (urlIndex != -1)
  51. {
  52. lines.RemoveAt(lines.Count - 1);
  53. this.Description = string.Join("\n", lines.ToArray()).TrimEnd('\n');
  54. this.LinkText = lastLine.Substring(0, urlIndex);
  55. this.Link = lastLine.Substring(urlIndex);
  56. return;
  57. }
  58. }
  59. this.Description = value;
  60. this.LinkText = "";
  61. this.Link = "";
  62. }
  63. private void Hyperlink_Click(object sender, RoutedEventArgs e)
  64. {
  65. Process.Start(new ProcessStartInfo(Link));
  66. e.Handled = true;
  67. }
  68. }
  69. }