How to add tool tip to "inline" hyperlink - c#

I have a TextBlock control in my UWP app and inside it I have several text and hyperlinks. I shorten the urls of the hyperlinks when showing to user but I want to add a tooltip that shows the full url when user hovers over hyperlink.
I know how to add tool tips to controls but in this case Hyperlink object is not a control (Windows.UI.Xaml.Documents.Hyperlink). Although the code below does not give any errors, it does not work either, it does not show the tool tip:
var textBlock = new TextBlock();
var link = new Hyperlink();
var run = new Run();
run.Text = "http://www.twitter.com/.../";
ToolTip toolTip = new ToolTip();
toolTip.Content = "http://www.twitter.com/blablabla/longurl/";
ToolTipService.SetToolTip(link, toolTip);
link.Inlines.Add(run);
textBlock.Inlines.Add(link);
Is there a way to add tool tips to Inline objects inside a TextBlock?

As you known, Hyperlink object is not a control, it seems like ToolTipService.ToolTip attached property take no effect on it. As a workaround I recommend you to use RichTextBlock instead of TextBlock. This is because RichTextBlock supports InlineUIContainer class at the same time TextBlock doesn't support. HyperlinkButton can be added to InlineUIContainer, since ToolTipService.ToolTip take effects on HyperlinkButton, so we can use a RichTextBlock with HyperlinkButton as follows to meet your requirements:
<RichTextBlock>
<Paragraph>
<Italic>This is an inline bing: </Italic>
<InlineUIContainer>
<HyperlinkButton Content="bing" NavigateUri="http://www.bing.com" VerticalAlignment="Bottom" Padding="0,-3" xml:space="preserve">
<ToolTipService.ToolTip>
<ToolTip
Content="Offset ToolTip."
HorizontalOffset="20"
VerticalOffset="30" />
</ToolTipService.ToolTip>
</HyperlinkButton>
</InlineUIContainer>
Mauris auctor tincidunt auctor.
</Paragraph>
</RichTextBlock>

You can do this with a HyperlinkButton:
<HyperlinkButton NavigateUri="http://www.mrlacey.com/" >
<TextBlock Text="mrlacey.com">
<ToolTipService.ToolTip>
<TextBlock Text="http://mrlacey.com/" />
</ToolTipService.ToolTip>
</TextBlock>
</HyperlinkButton>

Related

Having links in a binding

I'm binding text in LocalizedResources (for various languages) and I need to have links inside that text. Unfortunately when I bind it like this I can't have links in it:
<TextBlock TextWrapping="Wrap" Text="{Binding Path=LocalizedResources.AboutText, Source={StaticResource LocalizedStrings}}"/>
So I basically want to make some of the words in that block of text links. Is that possible?
EDIT: I need something like this
Except I'm binding this text from LocalizedResources as I said
As Romasz suggested, you should use RichTextBox instead of a TextBlock. You can solve the binding there with the following code:
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
For the hyperlinks, you can go through the whole text using C# and than separate the link from the text (you can use something like --- and than you know that whenever a --- appears in the text, you have a hyperlink).
Once you have a text and a hyperlink, you can add them to the RichTextBox using the following code:
Run myRun = new Run();
myRun.Text = "Displaying text with ";
Hyperlink link = new Hyperlink();
link.Inlines.Add("hyperlink");
link.NavigateUri = new Uri("https://stackoverflow.com/");
link.TargetName = "_blank";
link.Foreground = new SolidColorBrush(Colors.Blue);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(link);
myRun = new Run();
myRun.Text = " and with some text after the link.";
myParagraph.Inlines.Add(myRun);
rtb.Blocks.Add(myParagraph);
Where rtb is the name of my RichTextBox.
Straightforward solution would be as follows:
Split your text into few chunks, for example
. before link 1
. link 1
. before link 2
. link 2
. after link 2
create textblock for every before/after link section and add hyperlink button for links.
You should be able to blend thgether these parts, so that user won't distinguish text and links
Hope this helps
That is the code:
<TextBlock FontSize="20">
<Run x:Uid="one" />
<Run x:Uid="link_description" Foreground="BlueViolet">
<iact:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:OpenHyperlinkAction x:Uid="link" />
</core:EventTriggerBehavior>
</iact:Interaction.Behaviors>
</Run>
<Run x:Uid="two" />
</TextBlock>
Or:
<TextBlock FontSize="20">
<Run x:Uid="one" />
<Hyperlink x:Uid="link" Foreground="BlueViolet" >
<Run x:Uid="link_description" />
</Hyperlink>
<Run x:Uid="two" />
</TextBlock>

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

How to programatically add an image to a textblock?

I'm trying to set an icon to be attached to a textblock when it is saved but I'm not sure how I would do this programatically.What I'm thinking is you could set the value to the left of the textbox to an image.I have tried this but I'm not sure how I would set it to an image icon:
NoteGridView.SetValue(Canvas.LeftProperty(double)block.GetValue(Canvas.LeftProperty));
This is how I'm adding the notes to the layout:
// Creates a new textblock that for this note.
TextBlock block = new TextBlock();
//block.SetValue
notepadTxtBox.SetValue(Canvas.LeftProperty, (double)block.GetValue(Canvas.LeftProperty));
block.Width = 250;
block.Height = 125;
block.Text = notepadTxtBox.Text;
// Add that note to the grid.
NoteGridView.Items.Add(block);
Does anyone have any idea how I would acheive this or is it possible to do in the xaml code?
This might give a better understanding of what I'm trying to do:
You could put a StackPanel inside of the TextBlock, containing an Image, and another TextBlock, like so:
<TextBlock>
<StackPanel Orientation="Horizontal">
<Image Name="YourImage" Source="image/Under-Construction.png" />
<TextBlock Text="Your Text Block Text" />
</StackPanel>
</TextBlock>
You would then be able to set the Image programmatically like so:
YourImage.Source = "path.to.image.file"

Change AppBar's menu text

I have the following AppBar.
<Page.BottomAppBar>
<AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="switchMeasurementMode" AutomationProperties.Name = "Breath rate" Style="{StaticResource AppBarButtonStyle}" Click="switchMeasurementMode_Click" />
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
It looks like this
I tend to change its text during run-time with the following C# code
private void switchMeasurementMode_Click(object sender, RoutedEventArgs e)
{
this.switchMeasurementMode.Name = "111";
}
But the button text is not changed. Is there anything I had missed out?
If you use the default styles for AppBar in Windows 8 C# projects, then you have to change the attached property AutomationProperties.Name either in XAML using:
AutomationProperties.Name = "new name"
or in code using:
Button.SetValue(AutomationProperties.NameProperty, "new value");
or
AutomationProperties.SetName(Button, "new value");
The button is a content control. You set the Content property to change the content.
this.switchMeasurementMode.Content= "111";
The Name property is how you set the programmatic "handle" for the Button. You use the name in the code editor to modify the control. In your case, you are changing the name, which means you loose the ability to say this.switchMeasurementMode...
FYI, content can be more than just text. Most XAML elements can be added as content.

Making URLs on a textblock clickable

I'm programming a little Twitter Client just for fun. I have the tweet's text on a TextBlock and I want to make the URLs clickable.
I know that I have to parse the URLs with a regexp but... how I put text plus link on the TextBlock?
I can't have a string like: Hello check my blog at <Hyperlink>http​://myblogurl.com</Hyperlink> because the TextBlock doesn't parse the tags.
Then, how I can have a TextBlock that maybe has a link or maybe not?
Thank you.
<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
This is a richTextBox. And this is a <Hyperlink NavigateUri="http://www.microsoft.com">Hyperlink</Hyperlink>.
</Paragraph>
</FlowDocument>
</RichTextBox>
MSDN discussion
Rather than use a TextBlock, take a look at using the WPF version of the RichTextBox. It's a very flexible little critter.
Something like...
<TextBlock>
<Hyperlink Name="btnOpen" Click="btnOpen_Click">
<TextBlock Text="Click to Open" />
</Hyperlink>
</TextBlock>
You could parse the string in code behind and build up a collection of content controls, change your textblock to a wrap panel and set the children of the panel to your collection you have created.

Categories

Resources