简体   繁体   中英

Use hyperlink in a TextBlock

Code written below

<TextBlock FontFamily="Arial" Width="450"
    Text="Posted In" Foreground="Red"
    TextAlignment="Left" TextWrapping="Wrap">
    <Run Foreground="Red" Text="{Binding CategoryName}"></Run>
    <Run Foreground="Red" Text="{Binding CreatedOn}"></Run>
    <LineBreak/>
    <LineBreak/>
    <Run
        Foreground="Black"
        FontSize="24"
        Text="{Binding ArticleDescription}"></Run>
    <LineBreak/>
    <LineBreak/>
    <Run Foreground="Red" Text="Posted By"></Run>
    <Run Foreground="Red" Text="{Binding CreatedBy}"></Run>
    <Hyperlink Foreground="Red">sadas</Hyperlink>
</TextBlock>

Basically I used this code if I use hyperlink it crashes the application. If I don't use the hyperlink, the application works totally fine. How can I use a hyperlink in a textblock?

Error message:

Error HRESULT E_FAIL has been returned from a call to a COM component.

Set NavigateUri property in HyperLink Class (ie HyperLink control). For more http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink(v=vs.110).aspx

here is sample code

 <TextBlock x:Name="txtSource"  Text="abc"  Tap="LinkClicked" />

and in code behind LinkClicked event put this

  private void LinkClicked(object sender, System.Windows.Input.GestureEventArgs e)
    {


                        string url = "http://google.com";
                        WebBrowserTask wbt = new WebBrowserTask();
                        wbt.Uri = new Uri(url);
                        wbt.Show();
        }

hope this is what you where looking for

You can Use RichtextBox ether then textbox

<RichTextBox TextWrapping="Wrap">
    <Paragraph>
        <Run Text="John Doe" />
        <LineBreak />
        <Run Text="503 (Building DS 126)" />
        <LineBreak />
        <Run Text="tel.:  +30 210-1234567" />
        <LineBreak />
        <Hyperlink Click="Hyperlink_OnClick">e-mail:   johndoe@uni.gr</Hyperlink>
    </Paragraph>
</RichTextBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM