WPF RichTextBox - spell check does not work with formatted text - wpf

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.

Related

how to fix error "debugging resource strings are unavailable"

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>

Richtextbox: Embedd image and surround with text

I would like to embed an image in an Richtextbox in way that the image is sourrounded by text (should look like a newspaper article which contains some images).
That's what I have until now:
<RichTextBox>
<Paragraph>
<InlineUIContainer>
<Image Source="{Binding Image}" Width="200" Height="100" />
</InlineUIContainer>
<Run Text="{Binding Text}" />
</Paragraph>
</RichTextBox>
This code embedds the image in a way so that the first line of the run starts at the bottom right edge of the image.
But I would like to embedd it such a way:
http://i.stack.imgur.com/NVUNz.jpg
How can this be implemented for WP7.1?
Thanks.
I know this can be done, i believe you would be looking at either Margin or Padding around the image.....As the whole thing will look like an object because that's what it is.
so you want to put Padding or Margin around the image to push the text away from the object giving the appearance of space between the object. Also try Pushing the text to the right and the image to the left.
I hope this will help a little. :)
I don't think it can be done until FlowDocument will be available in Silverlight. Or somebody invents a similar control.

Silverlight TextBlock Text Attribute vs Content

When would I use the Text attribute of the <TextBlock> and when should I put my text in the content of the <TextBlock>?
<TextBlock Text="Example Text" />
vs.
<TextBlock>Example Text</TextBlock>
The former can be bound, whilst the latter is particularly useful when combining Runs:
<TextBlock Text="{Binding SomeProperty}"/>
<TextBlock>
<Run>You have </Run>
<Run Text="{Binding Count}"/>
<Run>items.</Run>
</TextBlock>
The use of the Text property has become common as a result of previous versions of the Xaml parser but the placing the text as content is more natural especially if you have a background in HTML.
The fact the many TextBlocks either have simple short chunks of literal text in or are bound. Would tip the balance IMO to using the Text property. In addition any globalisation that may come along latter may end with those literals being replaced by bindings as well.

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/XAML: Typography.Capitals seems to have no effect

All of these bits of text look the same, but I am trying to get them to look different. I want small caps text. What am I missing here to get the small caps typography effect to work?
To reproduce this, open Visual Studio 2008, Do File|New Project, create a new Windows|WPF application, paste the mark-up below into Window1.xaml, then run it.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Run>Some text</Run> <LineBreak />
<Run Typography.Capitals="SmallCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="AllSmallCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="PetiteCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="AllPetiteCaps">Some text</Run> <LineBreak />
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
</Grid>
</Window>
Based on the first answer, it seems that if you specify a particular font, you can get somewhere. Change the FlowDocument start tag to:
<FlowDocument FontFamily="Palatino Linotype">
.. and you get SmallCaps and AllSmallCaps, but not PetiteCaps or AllPetiteCaps. So it depends on the font. But this gives rise to other questions:
Why doesn't the default font (which looks a lot like Times New Roman) support these?
Do other widely used fonts (e.g. the local Courier New equivalent) support these?
Is there a list of which fonts support what?
What percentage of fonts will support this - most, some, or few?
Can you determine in code what the font supports - if this is the case, I could fake the AllSmallCaps - e.g. by converting the text to all capitals and scaling by 80%. But not SmallCaps.
This only works with specific OpenType fonts - the example in Help uses Pescadero which is in the Open Type Sample. Even then, only SmallCaps and AllSmallCaps are supported.
I noticed that default font with a "bold" fontweight does render the SmallCaps properly:
<StackPanel>
<TextBlock Typography.Capitals="SmallCaps" FontFamily="Pescadero" Padding="2">2pm</TextBlock>
<TextBlock Typography.Capitals="SmallCaps" FontWeight="Bold" Padding="2">2pm</TextBlock>
</StackPanel>

Resources