WPF: Use SpellCheck On Read-Only TextBox - wpf

I'm looking to display text with the wavey red lines where a word is misspelt, but I only want the text to be selectable, not editable. If I set the TextBox's IsReadOnly property to True or IsEnabled to False, the wavey red lines disappear and I can't get around it by putting something transparent as this will prevent the user being able to select sections of the text.
Is there anyway I can keep the red lines, allow the text to be selectable but prevent the actual text being modified?
Thanks

You could hook up to on the text change event of the text box, and just reject the new text. It would have the same effect of readonly without graying out the textbox or getting rid of the spell checking.

Thanks David. I'm currently looking at 2 possible solutions, yours and the following:
I've created a custom control which is based on the standard TextBox, but effectively has 2 textboxes laid on top of one another in this manor:
<TextBox Name="tbxBack"
Foreground="Transparent"
SpellCheck.IsEnabled="True"
TextWrapping="Wrap"
SnapsToDevicePixels="True"/>
<TextBox Name="tbxFront"
Background="Transparent"
TextWrapping="Wrap"
SnapsToDevicePixels="True"
IsReadOnly="True"/>
I think it's pretty straight forward what's going on here, but I'm concerned about the potential overhead this will cause.
The reason I'm looking into the double TextBox solution is that I'm worried if I try and cancel the event, it could end up with some sort of flashing in the control when the text is changed.

Related

wpf - how to keep a control from being selected through tabbing [duplicate]

How do I set tab ordering in WPF? I have an ItemsControl with some items expanded and some collapsed and would like to skip the collapsed ones when I'm tabbing.
Any ideas?
If you want to explicitly set the tab ordering for elements in your form, the following attached property is supposed to help:
<Control KeyboardNavigation.TabIndex="0" ... />
I say "supposed to help" as I haven't found it very reliable though I probably need to read more about how it is intended to be used. I only post this half baked answer because no one else mentioned this property.
Note that in Win RT, the property is just TabIndex="0".
You can skip elements in the tab sequence by setting KeyboardNavigation.IsTabStop on the element in XAML.
KeyboardNavigation.IsTabStop="False"
You can setup a trigger that would toggle this property based on the expanded state.
<Control KeyboardNavigation.TabIndex="0" ... /> Works perfectly fine...
For example-
<ComboBox Height="23"
Margin="148,24,78,0"
Name="comboBoxDataSet"
VerticalAlignment="Top"
SelectionChanged="comboBoxDestMarketDataSet_SelectionChanged"
DropDownOpened="comboBoxDestMarketDataSet_DropDownOpened"
KeyboardNavigation.TabIndex="0" />
<ComboBox Height="23"
Margin="148,56,78,0"
Name="comboBoxCategory"
VerticalAlignment="Top"
SelectionChanged="comboBoxDestCategory_SelectionChanged"
DropDownOpened="comboBoxDestCategory_DropDownOpened"
KeyboardNavigation.TabIndex="1" />
Will allow you to navigate through these two combo boxes using TAB key.
I think there is a much easier solution here,
at the top within your control or window or whatever, you could add:
KeyboardNavigation.TabNavigation="Cycle"
This also automaticaly ignores the collapsed tabs.
Another alternative that has worked for me in the past is to simply remove all explicit TabIndex statements, and let the controls use the order that they're declared in XAML work their magic.
This, of course, may require you to reorder your controls. But this is a simple copy-paste operation.
You can use KeyboardNavigation.TabNavigation="None" to completely skip the Tabbing for specific control.

TextBox Partially Editable

Is it possible to make the contents of a TextBox or a RichTextBox parially editable? For instance, I would like to have something that looks like the following:
<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" FontFamily="Courier New" Margin="10,0">
This text and anything before it isn't editable.
This Text is Editable
This text and anything after it is not editable.
</TextBox>
or something like this:
<RichTextBox AcceptsReturn="True" VerticalScrollBarVisibility="Auto" FontFamily="Courier New" Margin="10,0">
<FlowDocument>
<Paragraph>This text and anything before it is not editable.</Paragraph>
<Paragraph>This text is editable.</Paragraph>
<Paragraph>This text and anything after it is not editable.</Paragraph>
</FlowDocument>
</RichTextBox>
Ideally, I would be able to style the editable text different than the uneditable text. Before anybody tells me I shouldn't do this, I have a valid reason for doing it.
Thanks in advance.
You should be able to encapsulate the three controls into a scrollable region and resize your editable field as needed (i.e., the editable field has no scrolling, only the containing region).
It's not very pretty, especially if there isn't an easy way of doing it with XAML (I've never used it, so I can't say), but I would content it's far better than trying to hack this thing into (Rich)TextBox.
A textBox with its Template overrriden to hide its border and 2 textblocks of same look and feel put ahead and post the textbox in that Template can achieve what you seek.
Default textbox template which you can override is here...

WPF ComboBox using shortcut keys for selecting items

I have a combo box in WPF that uses the following data template. With help from the forum I was able to get this to display and behave properly.
When the drop down is open I would like the user to be able to type a letter and have the drop down skip to that selection. I have seen this implemented two ways, one where it bascially captures the keyboard input in a text box in the combo box and selects based on what the user has typed. This allows the user to wait and type more text that gets add to the criteria. The other is where the keyboard input doesn't appear to get captured anywhere, if yo type a letter, wait a couple seconds and type another letter it takes you to the selection starting with the second letter you typed. If you want to type multiple letters of the search criteria you have to type them quickly together.
I'd be happy with either approach. They can only select an item though that is in the list, so I don't really like the idea of putting an edit control on the combo box where they can type in whatever they want. I could do this and than validate their input, but would rather force them to select something that is in the list.
In this case it is a list of system colors, which is over 140 of them, so having a way to quickly get around the list is what I need.
Here is my data template that I am using.
<DataTemplate x:Key="ColorSelectionComboBox" DataType="ComboBox">
<StackPanel Orientation="Horizontal">
<Rectangle Width="16" Height="16" Margin="0,2,4,2">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color}"/>
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
Any ideas are appreciated.
ComboBox has built-in support for incremental search using the keyboard. I haven't actually used it (so I'm no good for help with advanced troubleshooting) but I know it's there.
It looks like you need to set the ComboBox's IsTextSearchEnabled property to true, and then set the TextSearch.TextPath attached property to the binding path of the text you want to search on (probably "Name" to correspond with your TextBlock text).

Problem with TextBlock in ScrollViewer

I'm writing a WP7 app and on a certain page I have a TextBlock with textwrapping. This TextBlock gets its text through binding. I placed a ScrollViewer around the TextBlock for when there are too many lines of text. Here's an example:
<ScrollViewer Margin="0,128,0,0" Name="Scroller">
<TextBlock x:Name="ItemContent" TextWrapping="Wrap" Text="{Binding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</ScrollViewer>
Now when I scroll to the end of the text, I still can scroll down half a page or more. I have trimmed my text both sides, so that's not the problem either. Is there a way to fix this?
Thanks!
There is limitation on all UIElements that they can't be more than 2048px in either dimension. This is to avoid excessive memory use when creating much more UI than can fit on the screen at once.
I'd advice splitting the text over multiple TextBlocks.
One alternative is to embed the text in a WebBrowser control but this can provide a substandard user experience.
For an example on alternative method see http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx

Multiline for WPF TextBox

I am developing an app for sending some feedback.
Basically I'm trying to make a TextBox for comments, but I'm used to the WinForms MultiLine=true. I've set MinLines to 3, which is getting there, but preferably I'd like it if the user is able to type wherever in this block - like press enter and do dot points sort of thing. For example:
- Item 1 blah
- Item 2 blahlb lahbvl d
But at the moment the text all stays on one line.
- Item 1 blah - Item 2 blahb blahb blah
These comments will then help fill the body of an email which is sent. It may be pointless if I can't easily keep the same formatting when putting this string into the email body string (so that it looks like it does when sent as it does when typed).
Can I achieve what I'm after or do I have to leave it as all text on one line?
Enable TextWrapping="Wrap" and AcceptsReturn="True" on your TextBox.
You might also wish to enable AcceptsTab and SpellCheck.IsEnabled too.
Also, if, like me, you add controls directly in XAML (not using the editor), you might get frustrated that it won't stretch to the available height, even after setting those two properties.
To make the TextBox stretch, set the Height="Auto".
UPDATE:
In retrospect, I think this must have been necessary thanks to a default style for TextBoxes specifying the height to some standard for the application somewhere in the App resources. It may be worthwhile checking this if this helped you.
Here is a sample XAML that will allow TextBox to accept multiline text and it uses its own scrollbars:
<TextBox
Height="200"
Width="500"
TextWrapping="Wrap"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"/>
The only property corresponding in WPF to the
Winforms property: TextBox.Multiline = true
is the WPF property:
TextBox.AcceptsReturn = true
or
<TextBox AcceptsReturn="True" ...... />
All other settings, such as VerticalAlignement, WordWrap etc., only control how the TextBox interacts in the UI but do not affect the Multiline behaviour.
Contrary to #Andre Luus, setting Height="Auto" will not make the TextBox stretch. The solution I found was to set VerticalAlignment="Stretch"

Resources