选择画笔不起作用

时间:2013-11-30 13:22:10

标签: c# .net wpf xaml

WPF-选择画笔不起作用,我在我的情况下选择'H'但是它不起作用。 这是我的代码:
XAML:

<TextBox Text="Hello" Height="49"  Name="textBox2" Width="547" />

C#

textBox2.SelectionStart = 0;
textBox2.SelectionLength = 1;
textBox2.SelectionBrush = Brushes.Red;

2 个答案:

答案 0 :(得分:1)

试试这个

        textBox2.Focus();
        textBox2.SelectionStart = 0;
        textBox2.SelectionLength = 1;
        textBox2.SelectionBrush = Brushes.Red;

答案 1 :(得分:0)

另一种解决方案是让TextBox认为它没有失去焦点。这样,您就不会实际将焦点移回TextBox。

要使其正常工作,您必须至少对TextBox设置一次焦点,例如用户输入初始文本时,或者从构造函数中调用textBox2.Focus()。

标记:

<TextBox Height="49" x:Name="textBox2" LostFocus="TextBox2_OnLostFocus" />

代码隐藏:

private void TextBox2_OnLostFocus(object sender, RoutedEventArgs e)
{
    e.Handled = true;
}