wpftoolkit icon indicating copy to clipboard operation
wpftoolkit copied to clipboard

MaterialTextField SelectAll Failing

Open ZionGates opened this issue 2 years ago • 3 comments

Greetings, I'm using the MaterialTextField and the SelectAll method and the SelectionLength mthod both fail to select the text in the control. I'm attempting to Select the text when the control gets focus.

Please advise.

ZionGates avatar Jul 29 '22 04:07 ZionGates

Hi,

You could always try to use your own MaterialTextField and call a SelectAll() on the MaterialTextBox's TextBox when the MaterialTextBox gets the focus: public class MyMaterialTextField : MaterialTextField { public MyMaterialTextField() { this.GotFocus += this.MyMaterialTextField_GotFocus; }

private void MyMaterialTextField_GotFocus( object sender, System.Windows.RoutedEventArgs e )
{
  this.Dispatcher.BeginInvoke( DispatcherPriority.Input, new Action( () =>
  {
    var textBox = e.OriginalSource as TextBox;
    if( textBox != null )
    {
      textBox.SelectAll();
    }
  }
     ) );

 
}

}

XceedBoucherS avatar Aug 10 '22 18:08 XceedBoucherS

Thanks, that worked, however that seems like a complex workaround.

ZionGates avatar Aug 12 '22 04:08 ZionGates

Yes, I know.

Even if the MaterialTextField derives from TextBox, it has a completely redefined template. So the MaterialTextField.SelectAll() method(from TextBox) won't work. We could internally call the SelectAll() method on the inner TextBox from the Template, but when ? You want it when the control gets focus, but someone else could want it for any other reasons. So the workaround here is for you to call it on the inner TextBox when you want it.

XceedBoucherS avatar Aug 12 '22 11:08 XceedBoucherS