コード例 #1
0
 /// <summary>
 /// Cancel the add hyperlink form
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OnClickHyperlinkCancel(object sender, RoutedEventArgs e)
 {
     // back to normal
     XAMLRichBox.IsReadOnly = false;
     rtbToolbar.IsEnabled   = true;
     linkEditor.Visibility  = Visibility.Collapsed;
     XAMLRichBox.Focus();
 }
コード例 #2
0
        /// <summary>
        /// save changes in hyperlink panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnClickHyperlinkOk(object sender, RoutedEventArgs e)
        {
            RichTextBox rtb          = XAMLRichBox as RichTextBox;
            string      hyperlinkURL = linkEditorText.Text;
            Hyperlink   hyperlink    = ExtractHyperLink();

            if (null != hyperlinkURL && 0 < hyperlinkURL.Length)
            {
                try
                {
                    var uri = new UriBuilder(hyperlinkURL).Uri;
                    if (null == hyperlink)
                    {
                        // creates the link
                        hyperlink = new Hyperlink(rtb.Selection.Start, rtb.Selection.End);
                    }

                    // set new URL
                    hyperlink.NavigateUri = uri;
                    hyperlink.TargetName  = "_blank";

                    // update tooltip
                    hyperlink.ToolTip = uri.ToString();
                }
                catch (Exception)
                {
                    // NO-OP
                }
            }
            else if (null != hyperlink)
            {
                // old text
                TextRange textRangeHyper = new TextRange(hyperlink.ElementStart, hyperlink.ElementEnd);
                string    oldText        = textRangeHyper.Text;

                // remove hyperlink
                rtb.Selection.Text = String.Empty;

                // insert plain text in its place
                new Run(oldText, rtb.Selection.Start);
            }

            // back to normal
            XAMLRichBox.IsReadOnly = false;
            rtbToolbar.IsEnabled   = true;
            linkEditor.Visibility  = Visibility.Collapsed;
            XAMLRichBox.Focus();
        }