Preventing Hyperlink inside WPF RichTextbox from being edited by end user - c#

I am having a WPf hyperlink inside a WPf Richtextbox , clicking on hyperlink results in opening of a popup.
But i wish to prevent the editing of hyperlink text.
Initially it looks like below
but when user edits the hyperlink it looks like this
So I wish to prevent this editing of hyerlink.

If you add in a container it cannot be edited (but removed as a whole):
<InlineUIContainer>
<TextBlock>
<Hyperlink NavigateUri="http://www.google.com/">
<Run Text="Google"/>
</Hyperlink>
</TextBlock>
</InlineUIContainer>
Would this be any good?

Related

RichTextBox setting bound property as a blank string when text is pasted into it

I'm having a problem with one of the RichTextBox's in a program I'm working on right now.
If I type into the box everything works as it should and the bound property is updated with the text that is typed.
If a user, however, pastes any text into the RichTextBox from anywhere (inside or outside the program) the bound property is loaded with a blank string and, the failsafe's I have in place to catch that, fire and don't allow the user to save the updated information.
Even if I type into the box after I've pasted into it, the string remains empty. I can't figure out what's going on.
I included my binding below. I don't believe the code that updates the value in the database is the issue so I will not include it for now.
<RichTextBox
x:Name="DescriptionBox"
Padding="3"
FontSize="14"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<FlowDocument>
<Paragraph>
<Run Text="{Binding SelectedReference.Description}" />
</Paragraph>
</FlowDocument>
</RichTextBox>
I don't believe the problem is with my binding as the text is updated when the user manually changes text in the box. The problem only occurs when the user pastes text into the box.

Hashtags in Binding TextBlock C#

I have a listview in my app (C# - UWP)
and in my DataTemplate i have a TextBlock that it Binding a text.
this is my code:
<TextBlock Text="{Binding Caption}" FontSize="11"/>
Now, how can i coloring all hashtags in the text? And clickable?
Note: all captions that binding this TextBlock is variable.
Like:
This is test #message for testing
Or
I like #German and #Russian language
I want change color #message, #German and #Russian and clickable feature in TextBlock
One option is to use a rich text box. Rich text box can render HTML like tags.
so you can have text like
<p> I am following the <a>#Russian-Language</a> <a>#azure</a> tutorials. </P>
Then anchor tags can have targets and they will be clickable. OR you can call a method on this hyperlink click.
Hope this helps you.
I made a control few months back called HashHandleTextBlock. The core concept of this is based on MarkdownTextBlock of UWP Community Toolkit.
Below is how you use controls.
<UnwantedControls:HashHandleTextBlock Text="{Binding ElementName=InputText, Path=Text}"
LinkForeground="DarkGray"
HashPrefix="https://twitter.com/hashtag/"
HandlePrefix="https://twitter.com/" />
You can also download the source from Github and modify control for your requirement.

How to display links in TextBlock?

I want to show a long list of reviews which may contain links. how can I show them so the user can click and open links?
this question has been asked a lot, but I couldn't find a functional answer, this obsolete answer seems isn't working:
<TextBlock>
<Run>let me</Run>
<Hyperlink NavigateUri="http://www.google.com">google</Hyperlink>
<Run>that for you</Run>
</TextBlock>
Also RichTextBox doesn't support Data Binding does it?
reviews will be shown inside a LongListSelector like this:
<phone:LongListSelector ItemsSource="{Binding Reviews}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text={Binding Review}/>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
You are missing Text Property,
Here is a complete example of how to add links inside RichTextBox
Hyperlink inside rich textbox

Copying Xaml RichTextBlock text with InlineUIContainers

I am working with some RichTextBlock objects that contain InlineUIContainer elements. I would like to be able to select and copy all of the text including the text contained in the InlineUIContainer.
Currently, when I select all of the text in the block, the text contained in the InlineUIContainer objects are skipped.
Here is an example of what I'm creating:
<RichTextBlock IsTextSelectionEnabled="True">
<Paragraph FontSize="20">
<Bold>This text is selectable</Bold>
<InlineUIContainer FontFamily="Global User Interface">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="11" VerticalAlignment="Top" Margin="0,0,-1,0">Super Script Text</TextBlock>
<HyperlinkButton ClickMode="Release" Style="{StaticResource NoMarginHyperlinkButtonStyle}">
Link
</HyperlinkButton>
</StackPanel>
</InlineUIContainer>
This text is also selectable
</Paragraph>
</RichTextBlock>
If I select all of the text from this piece of Xaml and copy/paste it in NotePad, I don't get the Super Script Text or the Link text.
Is there any way to get all of the text selected?
This is because HyperlinkButton is not part of the document API and in fact a UIElement wrapped in InlineUIContainer. There are 2 ways to handle this.
Switch to Windows 8.1 and Hyperlink which inherits from TextElement and copy will work just fine.
This is the hard way, if you must support this in Windows 8.
Remove the default Context menu items for the RichTextBlock and replace with your own Copy Command. Which should get the 2 TextPointers i.e. RichtextBlock.SelectionStart and RichTextBlock.SelectionEnd
Now with WPF we could get a TextRange within this range but winRT does not expose it, so you will need to do it in your code...
Get all the block within the RichTextBlock, and iterate through each to check if it's ContentStart and ContentEnd is within the RTB.SelectionStart and RTB.SelectionEnd if so then add them to a list.
Now it should be easy to extract all the Runs and Bold/Italics from this list and any InlineUIContainers hosting HyperlinkButtons.
2 This is not really a good way to go as it will be hard to allow for margins etc. on Paragraphs etc.

Textblock text selection

I have a chat window where each message is a TextBlock. I want to be able to select the text inside my TextBlocks. Google says to use a TextBox instead, which I cannot do because they do not support runs, which I am using to create hyperlinks inside my messages. What options do I have?
Check out the RichTextBox implementation for WPF.
How about making an CustomContentControl with TextBox (with selection style) and a Link in it and play with visibility based on content. Some thing like
<myControls:CustomContentControl>
<Grid>
// play with visibilty depending on content.
<CustomLink/>
<TextBox Style="{StaticResource SelectionStyle}"/>
</Grid>
</myControls:ContentControl>

Categories