Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:24:59+00:00 2026-05-24T07:24:59+00:00

I use a custom textblock in my WPF Application, when I use it in

  • 0

I use a custom textblock in my WPF Application, when I use it in WPF Windows it worked good but when I use it in a WPF Page it make a problem. When I click on a link in my Custom Control it browse the link and show in browser but the WPF page navigate back to another WPF Page too (first page)

namespace Dtwitter.Controls
{

public class TweetTextBlock : TextBlock
{

    public TweetTextBlock()
    {

    }

    #region Dependency properties

    public string TweetText
    {
        get { return (string)GetValue(TweetTextProperty); }
        set { SetValue(TweetTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TweetText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TweetTextProperty =
        DependencyProperty.Register("TweetText", typeof(string), typeof(TweetTextBlock),
        new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnTweetTextChanged)));

    #endregion



    private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        string text = args.NewValue as string;
        if (!string.IsNullOrEmpty(text))
        {
            TweetTextBlock textblock = (TweetTextBlock)obj;
            textblock.Inlines.Clear();
            textblock.Inlines.Add(" ");

            string[] words = Regex.Split(text, @"([ \(\)\{\}\[\]])");

            string possibleUserName = words[0].ToString();

            if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "@"))
            {
                textblock = FormatName(textblock, possibleUserName);
                words.SetValue("", 0);
            }

            foreach (string word in words)
            {
                // clickable hyperlinks
                if (UrlShorteningService.IsUrl(word))
                {
                    try
                    {
                        Hyperlink link = new Hyperlink();
                        link.NavigateUri = new Uri(word);
                        link.Inlines.Add(word);
                        link.Click += new RoutedEventHandler(link_Click);
                        link.ToolTip = "Open link in the default browser";
                        textblock.Inlines.Add(link);
                    }
                    catch
                    {
                        //TODO:What are we catching here? Why? Log it?
                        textblock.Inlines.Add(word);
                    }
                }
                // clickable @name
                else if (word.StartsWith("@"))
                {
                    textblock = FormatName(textblock, word);

                }

                // clickable #hashtag
                else if (word.StartsWith("#"))
                {
                    string hashtag = String.Empty;
                    Match foundHashtag = Regex.Match(word, @"#(\w+)(?<suffix>.*)");
                    if (foundHashtag.Success)
                    {
                        hashtag = foundHashtag.Groups[1].Captures[0].Value;
                        Hyperlink tag = new Hyperlink();
                        tag.Inlines.Add(hashtag);

                        string hashtagUrl = "http://search.twitter.com/search?q=%23{0}";

                        // The main application has access to the Settings class, where a
                        // user-defined hashtagUrl can be stored.  This hardcoded one that
                        // is used to set the NavigateUri is just a default behavior that
                        // will be used if the click event is not handled for some reason.

                        tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag));
                        tag.ToolTip = "Show statuses that include this hashtag";
                        tag.Tag = hashtag;

                        tag.Click += new RoutedEventHandler(hashtag_Click);

                        textblock.Inlines.Add("#");
                        textblock.Inlines.Add(tag);
                        textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value);
                    }
                }
                else
                {
                    textblock.Inlines.Add(word);
                }
            }

            textblock.Inlines.Add(" ");
        }
    }

    public static TweetTextBlock FormatName(TweetTextBlock textblock, string word)
    {
        string userName = String.Empty;
        string firstLetter = word.Substring(0, 1);

        Match foundUsername = Regex.Match(word, @"@(\w+)(?<suffix>.*)");

        if (foundUsername.Success)
        {
            userName = foundUsername.Groups[1].Captures[0].Value;
            Hyperlink name = new Hyperlink();
            name.Inlines.Add(userName);
            name.NavigateUri = new Uri("http://twitter.com/" + userName);
            name.ToolTip = "View @" + userName + "'s recent tweets";
            name.Tag = userName;

            name.Click += new RoutedEventHandler(name_Click);

            if (firstLetter != "@")
                textblock.Inlines.Add(firstLetter);

            textblock.Inlines.Add("@");
            textblock.Inlines.Add(name);
            textblock.Inlines.Add(foundUsername.Groups["suffix"].Captures[0].Value);
        }
        return textblock;
    }


    static void link_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString());
        }
        catch
        {
            //TODO: Log specific URL that caused error
            MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:25:00+00:00Added an answer on May 24, 2026 at 7:25 am

    change your link click method to

    static void link_click(Object sender, RequestNavigateEventArgs e) {
        try {
            System.Diagnostics.Process.Start(e.Uri.ToString());
        } catch {
            //TODO: Log specific URL that caused error
            MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        } finally {
           e.Handled = true;
        }
    }
    

    change your

    link.Click+=new RoutedEventHandler(link_Click);
    

    to

    link.RequestNavigate+=new RequestNavigateEventHandler(link_Click);
    

    Set e.Handled=true in link_click to mark you’ve dealt with the link click to prevent the framework from additionally processing your link click further.

    Alternatively you may be able to just set the TargetName property of Hyperlink to “_blank” and not need the process start command

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a custom control in a WPF app, and I have
I want to use Custom URL Scheme to transfer data from application to other
I am using wordpress and use custom permalink structure: /%category%/%postname%/ My problem is that
I have a custom control in WPF. In this I have a DependencyProperty of
I've got a simple custom control : namespace Application.Custom_Controls { public class ReadOnlyTextBox :
I am developing a WPF application, and have a TextBlock which I want to
To translate my WPF application I use a Markup extension which returns a Binding
In a WPF Custom Control template, is there any way that I can do
In WPF, I am creating a simple custom control for my TODO program. It
We use custom event tracking in our web application with Google Analytics to report

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.