12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace WpfDemo
- {
- public partial class DescriptionControl : UserControl
- {
- public string Header
- {
- get { return (string)GetValue(HeaderProperty); }
- set { SetValue(HeaderProperty, value); }
- }
- public static readonly DependencyProperty HeaderProperty =
- DependencyProperty.Register("Header", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
- public string Description
- {
- get { return (string)GetValue(DescriptionProperty); }
- set { SetValue(DescriptionProperty, value); }
- }
- public static readonly DependencyProperty DescriptionProperty =
- DependencyProperty.Register("Description", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
- public string Link
- {
- get { return (string)GetValue(LinkProperty); }
- set { SetValue(LinkProperty, value); }
- }
- public static readonly DependencyProperty LinkProperty =
- DependencyProperty.Register("Link", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
- public string LinkText
- {
- get { return (string)GetValue(LinkTextProperty); }
- set { SetValue(LinkTextProperty, value); }
- }
- public static readonly DependencyProperty LinkTextProperty =
- DependencyProperty.Register("LinkText", typeof(string), typeof(DescriptionControl), new PropertyMetadata(""));
- public DescriptionControl()
- {
- InitializeComponent();
- }
- public void SetDescription(string value)
- {
- var lines = value.Replace("\r\n", "\n").Split('\n').ToList();
- // check the last line for url
- if (lines.Count > 0)
- {
- var lastLine = lines[lines.Count - 1];
- int urlIndex = lastLine.IndexOf("http");
- if (urlIndex != -1)
- {
- lines.RemoveAt(lines.Count - 1);
- this.Description = string.Join("\n", lines.ToArray()).TrimEnd('\n');
- this.LinkText = lastLine.Substring(0, urlIndex);
- this.Link = lastLine.Substring(urlIndex);
- return;
- }
- }
- this.Description = value;
- this.LinkText = "";
- this.Link = "";
- }
- private void Hyperlink_Click(object sender, RoutedEventArgs e)
- {
- Process.Start(new ProcessStartInfo(Link));
- e.Handled = true;
- }
- }
- }
|