简体   繁体   中英

Insert Hyperlink in TextBlock

In a WPF application I load news from an RSS feed. The contents are displayed in a TextBlock. This TextBlock has a certain size. Contents are cut off by the TextTrimming method.

The only problem is I do not know exactly what position will be cut on my string. Is there a way to figure this out?

Can I prevent to cut off my HyperlinkButton?

Hyperlink hlink = new Hyperlink(new Run("here"));

myText.Inlines.Clear();
myText.Inlines.Add(value); //description from RSS Feed
myText.Inlines.Add(hlink);

为什么不通过替换StackPanel中的两个项而在文本后添加HyperLink?

Slightly more convenient is to use a :

<TextBlock>
    <Run Text="Short description"/>
    <Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>

If I understood your requirements this is one way to achieve your goals:

<StackPanel>  
    <DockPanel Width="200">
        <TextBlock DockPanel.Dock="Left"  Text="A short description."  TextTrimming="CharacterEllipsis"/>
        <TextBlock DockPanel.Dock="Right" TextAlignment="Right">
            <Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>   
        </TextBlock>
    </DockPanel>
    <DockPanel Width="200">
        <TextBlock DockPanel.Dock="Left" TextTrimming="CharacterEllipsis" MaxWidth="170" Text="A really long descripion of the item." />
        <TextBlock DockPanel.Dock="Right" TextAlignment="Right">
            <Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>   
        </TextBlock>
    </DockPanel>
</StackPanel>

在此处输入图片说明

So the DockPanel control might be a good candidate to consider.

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