how to fix error "debugging resource strings are unavailable" - silverlight

I hope this is not very stupid question. i have textblock within runs i want to add hyperlink.
Someting like this
<Hyperlink NavigateUri="http://google.com">
<Run Text="http://google.com"/>
</Hyperlink>
I tried different approaches, but every time i had to add hyperlink, an error occurred.
Error 2 [TextElementCollection_TypeNotSupportedInHost]
Arguments:
Debugging resource strings are unavailable.
Often the key and arguments provide sufficient information to diagnose the problem.

The TextBlock control does not support Hyperlink child controls.
You should use a RichTextBox instead, like this:
<RichTextBox IsReadOnly="True">
<Paragraph>
Displaying text with
<Hyperlink NavigateUri="http://www.google.com" TargetName="_blank">hyperlink</Hyperlink>.
</Paragraph>
</RichTextBox>

Related

Hyperlink in WPF Richtextbox

I load WPF Richtextbox contents from Xaml string in which there are some Hyperlinks. When it is loaded into control, Hyperlinks are not clickable! I want to click on them and their associated URL shows up.
No freschx, it's about WPF. A WPF RichTextBox, unlike the one in WinForms, does not have a DetectUrls property. And it's weird you wrote a Xaml code for that, even weirder there is someone who thought it useful.
Check this post out where JHubbard80 and me had two different approaches to solve this problem.
To make the Hyperlink, or any inline UIElement in general, available for hit testing, we must set RichTextBox.IsDocumentEnabled to true.
To make the Hyperlink clickable without pressing the Ctrl key, the Hyperlink must be made read-only e.g., by wrapping it into a TextBlock or by making the complete RichTextBox read-only (by setting RichTextBox.IsReadOnly to false).
<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
<Run Text="Some editable text" />
<TextBlock>
<Hyperlink NavigateUri="https://duckduckgo.com">
DuckDuckGo
</Hyperlink>
</TextBlock>
</Paragraph>
</FlowDocument>
</RichTextBox>
Ensure the DetectUrls property on the RichTextbox is set to true. You can then attach an event handler to the link clicked event and do what you wish.
<RichTextBox DetectUrls="True" />
Potential duplicate thread. Credit goes to Sam Meldrum from this thread.
For an even deeper analysis you may wish to try this article.

WPF TextWrapping on white spaces

I am using a TextBlock in WPF, on which I want to use TextWrapping, What I have done in XAML is as follows.
<TextBlock TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="0" Margin="5,5,5,5">
<TextBlock.Text>
This is just a test. This is just a test.
</TextBlock.Text>
</TextBlock>
When I run this code it shows output like this
This is just a test. Th
is is just a test.
But I only want wrapping on white spaces like
This is just a test.
This is just a test.
I am unable to find why WPF is showing this behavior, I have tried WrapWithOverflow and IsHyphenationEnabled=true with no success. Please help me out?
Bizz is right, this shouldn't be the behavior of Text Wrapping, Just a wild guess, are you copy/pasting text from some where to Visual studio? there might be an issue with white spaces in that case, and WPF will wrap your text in wrong way.

Superscript / subscript in hyperlink in WPF

I'm trying to make a Hyperlink that contains text with super- and/or subscripts. I've found two ways to do this, and both of them suck.
Solution #1: use Typography.Variants. This gives a terrific superscript... for some fonts.
<StackPanel>
<TextBlock FontFamily="Palatino Linotype" FontSize="30">
<Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
(Palatino Linotype)
</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="30">
<Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
(Segoe UI)
</TextBlock>
</StackPanel>
(source: excastle.com)
Looks beautiful in Palatino Linotype; but for fonts that don't support variants, it's simply ignored, no emulation is done, and the text is full-size, at-baseline, 100% normal. I would prefer to allow my end-users to select the font they want to use, and still have super/subscripts work.
Solution #2: use BaselineAlignment. This raises or lowers the text appropriately, though unlike solution #1, I have to decrease the font size manually. Still, it's effective for all fonts. The problem is the Hyperlink's underline.
<TextBlock FontSize="30" FontFamily="Palatino Linotype">
<Hyperlink>
R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
</Hyperlink>
</TextBlock>
The underline is raised and lowered along with the text, which looks pretty wretched. I'd rather have a continuous, unbroken underline under the whole Hyperlink. (And before anyone suggests a Border, I'd also like the Hyperlink to be able to word-wrap, with all of the words underlined, including the first row.)
Is there any way to make superscript and subscript work in WPF, in any font, without looking laughably bad when underlined?
If the hyperlink isn't going to wrap to more than one line, then embedding another TextBlock can work:
<TextBlock FontSize="30" FontFamily="Palatino Linotype">
<Hyperlink>
<TextBlock>
R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
</TextBlock>
</Hyperlink>
</TextBlock>
This will give a solid hyperlink under the Hyperlink's child, which means an unbroken hyperlink:
However, if the embedded TextBlock needs to wrap to multiple lines, you'll only get one underline under the entire wrapped paragraph, rather than underlining each line of text:
(source: excastle.com)
If you can put a TextBlock only around a short bit of content that needs superscripts -- e.g., around just the R^2 in the above example -- and leave the rest of the text parented to the hyperlink, then you get underlining as normal. But sometimes that's not practical, so it's something to watch out for.
You can use the superscript unicode characters (e.g. http://www.fileformat.info/info/unicode/char/b2/index.htm)
Like this:
<TextBlock FontSize="30" FontFamily="Segoe UI">
<Hyperlink>
Apply R² Calculation
</Hyperlink>
</TextBlock>
Result:
Obviously this will not work unless what you are super scripting actually has a unicode superscript character.

WPF: How to embed button inside text flow (wrap text around button)?

I'd like an advice to the following problem: I want to embed a Button into a text flow, but when I embed a Button and Label (or TextBlock) into the WrapPanel, I get the first figure:
alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png
I think that one of solutions could be FlowDocument, but I feel that this is far too heavy for a control simple like this (which could be used in several hundred instances). Do you have some other ideas about how to implement this? Thank you!
EDIT:
One solution could be the following (I didn't know it was possible to put more stuff into TextBlock), but I would lose the ability to bind (which I need):
<TextBlock TextWrapping="Wrap">
<Span>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
<!--
Problem: binding makes following error:
A 'Binding' cannot be set on the 'Text' property of type 'Run'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
-->
</Span>
</TextBlock>
To bind to Run.Text, checkout the BindableRun class by Fortes. Simple to implement, I use it all over my projects.
I found that implementing BindableRun correctly is pretty tricky - and almost all other available implementations will cause an exception from wpf layouting engine when the bound content changes from null to something non-null - see this problem, keyword "Collection was modified; enumeration operation may not execute."
Corrrect implementation from Microsoft is here - it shows how tricky this really is.
Solution: BindableRun class + the following markup:
<TextBlock>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
Funny thing it works on the designer of a UserControl...
In that case, using the Property Change of your control to set the value to the Run is enough. I mean, if you had something like:
<TextBlock>
<Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>
Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.

WPF RichTextBox - spell check does not work with formatted text

If I define a RichTextBox as follows;
<RichTextBox SpellCheck.IsEnabled="True">
<FlowDocument />
</RichTextBox>
When I type in the work 'Sample' and make the first three letters bold, the spell checker underlines the word.
The source XAML of the document shows that the RichTextBox is splitting the word into two seperate runs;
<Paragraph>
<Run FontWeight="Bold" xml:lang="en-gb">Sam</Run>
<Run xml:lang="en-gb">ple</Run>
</Paragraph>
If I manually contruct a document with the following blocks;
<FlowDocument>
<Paragraph>
<Run FontWeight="Bold">Sam</Run>ple
</Paragraph>
</FlowDocument>
The spell checker passed the word successfully.
Has anyone come across this before? Is there a workaround that I can use?
Thanks
Matt
There seem to be issues with the spell checker and different locales.
If I start with this:
<RichTextBox SpellCheck.IsEnabled="True" xml:lang="en-GB">
<FlowDocument />
</RichTextBox>
I can reproduce your error (by typing "Sample" and bolding the "Sam"), but not with this:
<RichTextBox SpellCheck.IsEnabled="True">
<FlowDocument />
</RichTextBox>
Someone has a similar problem here. Microsoft replies:
This problem occurs because the Language property on FrameworkElement (and thus TextBox/RichTextBox) defaults to "en-US", and you are using the "en-NZ" locale. When you type text into TextBox/RichTextBox, it will be in a different locale than the text set in XAML. The spell checker does not cross language boundaries, which results in the behavior you see.

Resources