I have a WPF RichTextBox control that I want to behave like a normal RichTextBox in every way except that the user cannot edit the text and the caret is not visible. The issue I'm having is that when I change IsReadOnly to False, the RichTextBox stops reacting to keystrokes. I want it to respond normally to keystrokes like Home, End, PgUp, PgDn, Up, Down. The documentation doesn't mention this behavior.
<RichTextBox
IsReadOnly="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Width="65" Height="20">
<FlowDocument>
<Paragraph>
Line 1
Line 2
Line 3
</Paragraph>
</FlowDocument>
</RichTextBox>
I've tried adding an OnKeyDown event handler, but it doesn't fire. I've watched the events in Snoop as I push keys with the RichTextBox in keyboard focus and no fired events are shown in Snoop. I've tried setting IsDocumentEnabled explicitly to True, but that also has no effect. I've tried wrapping the RichTextBox in a ScrollViewer, but this has no effect either.
Any suggestions for how I can maintain this RichTextBox as IsReadOnly=True, but get the expected keystroke behavior of a RichTextBox?
Set IsReadOnlyCaretVisible to True
<RichTextBox IsReadOnly="True"
IsReadOnlyCaretVisible="True"
... />
If you don't want the caret to be visible then set the CaretBrush to Transparent
<RichTextBox IsReadOnly="True"
IsReadOnlyCaretVisible="True"
CaretBrush="Transparent"
... />
Related
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.
I have a simple control that has a masked text box:
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
...
<extToolkit:MaskedTextBox Mask="000-000-000" Text="{Binding SerialNumber, UpdateSourceTrigger=PropertyChanged}" />
I also have a key binding on the control:
<UserControl.InputBindings>
<KeyBinding Command="{Binding SearchCommand}" Gesture="Enter" />
</UserControl.InputBindings>
The problem is when SearchCommand is executed I need the value they entered in the masked text box as the criteria for the search. With a regular text box this is no problem but apparently the MaskedTextBox control doesn't play well with PropertyChanged UpdateSourceTrigger.
If I click someplace else (so it looses focus) and then press enter it works, but obviously I don't want to have to do that. Are there any good workarounds for this situation?
You must bind your property to the Value property not the Text.
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Documentation
As the title states, I have a usercontrol with a textbox inside. The purpose of the user control is to add a spell check button and character count below the textbox. But for the most part, I'm using it just like a normal textbox:
<controls:SuperTextBox Text="{Binding Accomplishments, Mode=TwoWay, ValidatesOnNotifyDataErrors=True,NotifyOnValidationError=True}"
Height="200" EnableCharacterCounting="True" EnableSpellChecking="True" AcceptsReturn="True"
TextWrapping="Wrap" />
The problem I'm having is that I no longer get the nice red border when validation fails for the field. What do I need to do to reenable that behavior?
What you need to do here, it's to relay the validation of your UserControl back to the textbox.
I did answer a similar question here.
when hovering my mouse over a wrapped textbox, I want to get the word or text position immediately under the mouse.
I've seen some samples for a single textbox, like this one, but I have a wrapped textbox.
I think I might be able to do this if I had a MeasureString function but I don't have that either in Silverlight (would be useful for other things).
Example TextBox
<Textbox TextWrapping="Wrap" Width="50" Text=" ... " />
There isn't this ability in Silverlight today. Here's what I would do as a workaround:
Have an invisible (Opacity 0, IsHitTestVisible=False) WrapPanel with a TextBlock for each word.
Make the WrapPanel the same size as the TextBlock
Adjust margins on the TextBlocks and WrapPanels until you get similar spacing.
Do hit testing on the WrapPanel with the TextBlocks to determine the word
I have a FlowDocument that is defined in XAML like this;
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph>
<Run>Hello</Run>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
What I'd like to do is prevent the Find and Zoom controls from appearing when you press Ctrl-F or F3.
Is this possible?
Thanks in advance!
Matt
You could handle the PreviewKeyDown event for this control, and if either Ctrl-F or F3 is pressed, mark the event as handled. This should prevent the control itself from responding to that event and displaying the Find and Zoom controls.