如何在 Windows Phone 7 中突出显示文本框中的文本 [英] How to highlight text in textbox in Windows Phone 7

查看:16
本文介绍了如何在 Windows Phone 7 中突出显示文本框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows Phone 项目中,我有以下场景:

In a Windows Phone project I have the following scenario:

  1. 用户在文本框中键入一些文本
  2. 用户按下按钮禁用文本框并启动进程
  3. 对文本进行了一些处理,在处理的每一步中,我都想突出显示文本框中文本的某些部分(文本本身没有变化).
  4. 处理完成后启用文本框.

这样做的正确方法是什么?

What is the right way to do this?

目前我尝试设置一些选择背景并使用文本框 Select 方法设置所选文本,但即使启用了文本框,也没有选择的视觉指示.SelectedText 属性返回正确的选定文本,但至少在模拟器中没有任何视觉变化.

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.

这是我使用的无效代码:XAML

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>

背后的代码

private void btnTest_Click(object sender, RoutedEventArgs e)
{
    txtTest.Select(1, 1);
    //on this line SelectedText has the correct value
}

我愿意接受其他方式来做到这一点.我真的不想使用选择,因为在语义上这不是选择,但我觉得这是实现我想要的最简单的方法.我可能会使用其他方法来突出显示特定字符​​,例如使字体变大.我也可以隐藏文本框并用看起来相同的 TextBlock 替换它,但我觉得应该有更简单的方法来实现这一点.

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?

推荐答案

经过研究,我发现 TextBox 上的 Select 方法仅在 TextBox 具有焦点时提供视觉指示.TextBox 在未禁用时无法获得焦点,因此它对我没有用.RitchTextBox 是只读的,用于选择文本部分的 API 比我使用的解决方案更复杂.

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.

我使用带有以下代码的 TextBlock 来突出显示文本:

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);
}

当我处理输入时,我使 TextBox 不可见而 TextBlock 可见.当用户提供输入时,我会切换两者.我不确定这是否是正确的方法,我对其他解决方案持开放态度,但到目前为止,这是我找到的最简单的方法.

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.

这篇关于如何在 Windows Phone 7 中突出显示文本框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆