How to highlight text in textbox in Windows Phone 7

こ雲淡風輕ζ 提交于 2020-02-06 06:29:30

问题


In a Windows Phone project I have the following scenario:

  1. The user types some text in a textbox
  2. The user presses a button which disables the textbox and starts a process
  3. Some processing of the text is done and on every step of the processing I want to highlight certain part of the text in the textbox (the text itself does no change).
  4. The textbox is enabled after the processing is complete.

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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9549422/how-to-highlight-text-in-textbox-in-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!