开发者

Insert Hyperlink in TextBlock

开发者 https://www.devze.com 2023-03-24 06:31 出处:网络
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.

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.

Now I would like at the en开发者_JAVA百科d of each TextBlock to insert a hyperlink button. 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?

When I insert my Text in my TextBlock and then my Hyperlink-Button, my HyperlinkButton will be cut of. Can I prevent to cut off my HyperlinkButton?

XAML-Code:

<TextBlock Name="myText" />

C#-Code:

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

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


Why not just add the HyperLink after the text, by replacing both items in a StackPanel?


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>

Insert Hyperlink in TextBlock

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


Slightly more convenient is to use a :

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

精彩评论

暂无评论...
验证码 换一张
取 消