Question

I'm trying to select the whole text in a dialog but I'm not able to do so.

I have a class Participant which has a Property Firstname. When I set the participant in my dialog I'm calling a Focus-Method. However, when the dialog is open it's only in focus but not selected.

This is my Focus-Method:

public void FocusSurname()
{
        SurnameBox.SelectAll();
        SurnameBox.Focus();
        Keyboard.Focus(SurnameBox);
}

In the dialog I'm setting my participant as follow:

Participant Participant
    {
        get { return _participant; }
        set
        {
           _participant = value;
           FocusSurname();
        }
 }

My dialog open sourcecode is pretty much:

 public void ShowDialog(object owner)
 {
     Owner = owner as Window;
     ShowDialog();
 }

Why is the text not selected? :(

Even when I call FocursSurname in my ShowDialog-Method nothing is changing.

Was it helpful?

Solution

what you are trying to accomplish is view related code (in my opinion). The mvvm guidiance or pattern use (or at least my understanding of it) state that you want to use behaviors or attached properties for that. Means extending xaml functionality to plugin view related behaviors...

I found a stackoverflow question that relates to your topic. Check out if this might help... or try to derive from that solution...

Link:

Initial Focus and Select All behavior

HTH

OTHER TIPS

Bind following event handler method to GotFocus event of the text box

private static void SelectText(object sender, RoutedEventArgs e)
{
    var textBox = e.OriginalSource as TextBox;

    Keyboard.Focus(textBox);
    textBox.SelectAll();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top