Reputation: 23551
In a Windows Phone project I have the following scenario:
What is the right way to do this?
Currently I have tried to set some selection background and set the selected text using the textbox Select method but there is no visual indication of the selection even when the textbox is enabled. The SelectedText property returns the correct selected text but at least in the emulatior nothing changes visually.
Here is the code I use that does not work: XAML
<TextBox Name="txtTest" AcceptsReturn="True" Height="250" TextWrapping="Wrap" SelectionBackground="Red"></TextBox>
<Button Name="btnTest" Width="200" Click="btnTest_Click">Test</Button>
Code behind
private void btnTest_Click(object sender, RoutedEventArgs e)
{
txtTest.Select(1, 1);
//on this line SelectedText has the correct value
}
I am open to other ways to do this. I don't really want to use the selection since semantically this is not a selection but I felt it was the easiest way to achieve what I want. I may use other means to highlight specific characters for example making the font size bigger. I may also hide the textbox and replace it with a TextBlock that looks the same but I feel like there should be easier way to achieve this.
So what is the right way to implement this functionality?
Upvotes: 0
Views: 2696
Reputation: 23551
After a research I found out that the Select method on a TextBox only provides visual indication if the TextBox has focus. The TextBox cannot have focus when it is not disabled so it was of no use to me. The RitchTextBox is read only and the API to select parts of the text is more complex than the solution I used.
I used a TextBlock with the following code to highlight the text:
private void SetTextBlock(TextBlock textBlock, string text, int selectedIndex)
{
textBlock.Inlines.Clear();
Run previous = new Run();
previous.Text = text.Substring(0, selectedIndex);
Run current = new Run();
current.Text = text.Substring(selectedIndex, 1);
current.Foreground = new SolidColorBrush(Colors.Green);
current.FontWeight = FontWeights.ExtraBold;
Run next = new Run();
next.Text = text.Substring(selectedIndex + 1, text.Length - selectedIndex - 1);
textBlock.Inlines.Add(previous);
textBlock.Inlines.Add(current);
textBlock.Inlines.Add(next);
}
When I am processing the input I make the TextBox invisible and the TextBlock visible. When the user is providing input I switch the two. I am not sure if this is the right way to do it and I am open to other solutions but so far this is the easiest I have found.
Upvotes: 0
Reputation: 65564
If you had a single instance to highlight you could use the SelectionStart
and SelectionLength
properties.
If you have to highlight multiple instances then you could replace the TextBox with a RichTextBox
and indicate highlights by styling it appropriately. This won't allow you to show the highlighting while in edit mode though.
If you need to enable highlighting of text while editing it you'll need to create your own replacement/alternative TextBox.
Upvotes: 1