Search code examples
c#wpftextboxfocusselection

WPF TextBox's IsInactiveSelectionHighlightEnabled property is not work?


WPF's TextBox has a property named IsInactiveSelectionHighlightEnabled. I set this property to true in order to make a TextBox always show selection. However, it doesn't work in this case: enter image description here

    private void button_Click(object sender, RoutedEventArgs e) {
        textBox.Select(0, 10);
    }

I just want to see the selection after clicking the button. But selection will not appear until I right click the TextBox. Why? Am I miss something?


Solution

  • You should have the Keyboard focus on your textbox to select the text in it.

    Add this code to your button click event before the selection.

    Keyboard.Focus(textBox);  
    

    Hope it helps.