how to make Textbox text as Hyperlink in WPF

kida picture kida · Jul 15, 2013 · Viewed 11.3k times · Source

I want to make textbox text as Hyperlink. Suppose if the type www.google.com as tetxbox text,when i click on the text ,it show open the link in a browser..

I could not get any idea to implement this...can u suggest me any idea...i tried the two ways.

way1:

  <TextBox Grid.Row="4" Name="txtWebPage" VerticalAlignment="Top" TextDecorations="UnderLine" TextChanged="txtWebPage_TextChanged" Foreground="Blue">
                                </TextBox>

way2:

 <TextBlock Name="tbWebpage" Grid.Row="4" Background="White" VerticalAlignment="Top" Height="20" >
                                    <Hyperlink></Hyperlink>
                                </TextBlock>

way3:

   <RichTextBox Grid.Row="4" Name="rtxtWeb" BorderBrush="Gray" BorderThickness="1" VerticalAlignment="Top" Height="20" IsDocumentEnabled="True" Foreground="Blue" LostFocus="rtxtWeb_LostFocus">
                                    <FlowDocument>
                                        <Paragraph>
                                            <Hyperlink NavigateUri=""/>
                                        </Paragraph>
                                    </FlowDocument>
                                </RichTextBox>

I couldnt get how can i bind the RichTextBox text to Hyperlink uri!!! There is no click event for richtextbox...any suggestion please...

Answer

josh2112 picture josh2112 · Aug 23, 2013

First, I'm not sure why you'd want to do this... if the text becomes a clickable hyperlink the instant it's a valid URI, how would you continue to edit it?

The Hyperlink control doesn't do anything special for you and it can't be hosted in a TextBox. Instead, use a regular TextBox, check the text for a valid URI every time it's updated, and apply a style to make the text look like a clickable link.

<TextBox TextChanged="TextBox_TextChanged" MouseDoubleClick="TextBox_MouseDoubleClick">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidURI}" Value="True">
                    <Setter Property="TextDecorations" Value="Underline"/>
                    <Setter Property="Foreground" Value="#FF2A6DCD"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Every time the text is changed, TextBox_TextChanged is called. This checks whether the text is a valid URI using Uri.TryCreate(). If so, a property HasValidURI is set to true. A DataTrigger in the TextBox's style picks this up and makes the text underlined and blue.

Making the hyperlink immediately clickable would cause you not to be able to position the cursor, so I watch for a double-click instead. When one is received, convert the text to a URI again and kick off a Process with that URI.

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _hasValidURI;

    public bool HasValidURI
    {
        get { return _hasValidURI; }
        set { _hasValidURI = value; OnPropertyChanged( "HasValidURI" ); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged( string name )
    {
        var handler = PropertyChanged;
        if( handler != null ) handler( this, new PropertyChangedEventArgs( name ) );
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void TextBox_TextChanged( object sender, TextChangedEventArgs e )
    {
        Uri uri;
        HasValidURI = Uri.TryCreate( (sender as TextBox).Text, UriKind.Absolute, out uri );
    }

    private void TextBox_MouseDoubleClick( object sender, MouseButtonEventArgs e )
    {
        Uri uri;
        if( Uri.TryCreate( (sender as TextBox).Text, UriKind.Absolute, out uri ) )
        {
            Process.Start( new ProcessStartInfo( uri.AbsoluteUri ) );
        }
    }
}