satish
satish

Reputation: 31

Hyperlinks in TextBlock in windows phone 8.0 using C#

how to generate hyperlink from text writeen in textbox to textblock in windows phone 8.0 using C#

ex:- i entered

www.google.com in textbox and clicked on button after button click

the result should be

www.google.com
with hyperlink in textblock

Upvotes: 2

Views: 2944

Answers (4)

pranavjayadev
pranavjayadev

Reputation: 929

Try this

xaml

 <StackPanel x:Name="stack">
                <TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBox x:Name="txtInput"></TextBox>
                <Button Content="Create Link" Click="Button_Click"/>
                <RichTextBox x:Name="textBox" ></RichTextBox>
            </StackPanel>

and the button click in the cs file

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink hyperlink = new Hyperlink();
            hyperlink.Inlines.Add(txtInput.Text);

            Paragraph myParagraph = new Paragraph();
            myParagraph.Inlines.Add(hyperlink);
            textBox.Blocks.Add(myParagraph);
        }

Upvotes: 0

Romasz
Romasz

Reputation: 29792

You can easily put Hyperlink into RichTextBlock (in WP8.1 Runtime). I've also put Run in hyperlink so it's easier to manage its content. Example:

<StackPanel>
  <TextBox Name="myTextBox" Width="200"/>
  <RichTextBlock TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center">
    <Paragraph>
        <Run Text="This is a link to google:"/>
        <LineBreak/>
        <Hyperlink x:Name="myhyperlink" Click="myhyperlink_Click">
            <Run x:Name="hyperText" Text="textInside"/>
        </Hyperlink>
        <LineBreak/>
        <Run Text="you can click it to invoke doEvent in your code."/>
    </Paragraph>
  </RichTextBlock>
</StackPanel>

In the code behind - some logic example:

public MainPage()
{
   this.InitializeComponent();
   myTextBox.TextChanged += (sender, e) => hyperText.Text = myTextBox.Text;
}

private async void myhyperlink_Click(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
  await Windows.System.Launcher.LaunchUriAsync(new Uri(@"http://" + myTextBox.Text));
}

Note that in WP8.0 and WP8.1 Silverlight you will have to use RichTextBox with IsReadOnly = true

Upvotes: 2

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

try this:

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
    <StackPanel x:Name="stack">
        <TextBox x:Name="txtInput"></TextBox>
        <Button Content="Create Link" Click="Button_Click"/>
    </StackPanel>
</Grid>

CS:

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (txtInput.Text != "")
    {
        HyperlinkButton obj = new HyperlinkButton();
        obj.NavigateUri = new Uri(txtInput.Text,UriKind.RelativeOrAbsolute);
        obj.Content = txtInput.Text;
        obj.TargetName = "_blank";
        this.stack.Children.Add(obj);
    }
}

for e.g. try with http://google.com in textbox

Upvotes: 0

Harsha Bhat
Harsha Bhat

Reputation: 738

Use a HyperlinkButton control.

<HyperlinkButton NavigateUri="http://www.google.com">
            <HyperlinkButton.Content>
                   <TextBlock Text="google.com" />
            </HyperlinkButton.Content>
</HyperlinkButton>

Upvotes: 1

Related Questions