0

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.

drahnr
  • 6,782
  • 5
  • 48
  • 75
Frame91
  • 3,670
  • 8
  • 45
  • 89

2 Answers2

1

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

Community
  • 1
  • 1
silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • Thanks for the answer. In fact I'm already using a behavior for this issue but this is not quite solving my problem. - If I lose the focus and regain it, the text within the textbox is selected like I wished. But I can't figure out how to get the text selected at the beginning when my dialog pops up... The problem here is that my TextBox will be notified by a PropertyChangedEvent and this, unfortunately, more than once. First with the default-value and then with my desired-value - which should be selected. I can't figure out how to solve this :( – Frame91 May 02 '14 at 11:23
0

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();
}
voddy
  • 950
  • 1
  • 11
  • 21