Gonzalo Borghi

Gonzalo Borghi

  • NA
  • 4
  • 2.2k

Focus textbox after async call -

Oct 21 2016 10:02 PM

WPFHi, I have a strange problem. After using an asynchronous call, I can not do focus to a particular control (textbox in this example). If I comment the lines where asynchronous calls, the focus works perfectly. i'm use MVVM Light 5.3.0 Why? Thx!

  1. <TextBox Grid.Row="0"  
  2.          Grid.Column="1"  
  3.          utilidades:FocusExtension.IsFocused="{Binding IsFocused}"  
  4.          Text="{Binding Articulo.Codigo, UpdateSourceTrigger=PropertyChanged}" />  
  5.   
  6. private async void CargarRubros()  
  7. {  
  8.     PrepareIsBusy();  
  9.   
  10.     ShowIsBusyMessage = true;  
  11.   
  12.     ActiveProgressRing = true;  
  13.   
  14.     IsBusyCustomMessage("Cargando rubros...");  
  15.   
  16.     try  
  17.     {  
  18.         Rubros =  
  19.             await  
  20.                 Task.Factory.StartNew(() => new ObservableCollection<Rubro>(_rubroService.TodosLosSubrubros()));  
  21.   
  22.         ActiveProgressRing = false;  
  23.   
  24.         if (Rubros.Any())  
  25.         {  
  26.             RubroSeleccionado = Rubros.First();  
  27.         }  
  28.         else  
  29.         {  
  30.             IsBusyInfoMessage("No se encontraron rubros.");  
  31.   
  32.             await Task.Factory.StartNew(() => Thread.Sleep(2000));  
  33.         }  
  34.     }  
  35.     catch (Exception)  
  36.     {  
  37.         IsBusyErrorMessage("Error al intentar obtener los rubros.");  
  38.   
  39.         ActiveProgressRing = false;  
  40.   
  41.         await Task.Factory.StartNew(() => Thread.Sleep(2000));  
  42.     }  
  43.     finally  
  44.     {  
  45.         ShowIsBusyMessage = false;  
  46.   
  47.         ResetMessageAndImage();  
  48.   
  49.         PrepareIsBusy();  
  50.   
  51.         IsFocused = true;  
  52.     }  
  53. }  
  54.   
  55. public static class FocusExtension  
  56. {  
  57.     public static bool GetIsFocused(DependencyObject obj)  
  58.     {  
  59.         return (bool)obj.GetValue(IsFocusedProperty);  
  60.     }  
  61.   
  62.     public static void SetIsFocused(DependencyObject obj, bool value)  
  63.     {  
  64.         obj.SetValue(IsFocusedProperty, value);  
  65.     }  
  66.   
  67.     public static readonly DependencyProperty IsFocusedProperty =  
  68.         DependencyProperty.RegisterAttached(  
  69.             "IsFocused"typeof(bool), typeof(FocusExtension),  
  70.             new UIPropertyMetadata(falsenull, OnCoerceValue));  
  71.   
  72.     private static object OnCoerceValue(DependencyObject d, object baseValue)  
  73.     {  
  74.         if ((bool) baseValue)  
  75.         {  
  76.             ((UIElement) d).Focus();  
  77.         }  
  78.         else if (((UIElement) d).IsFocused)  
  79.         {  
  80.             Keyboard.ClearFocus();  
  81.         }  
  82.         return (bool)baseValue;  
  83.     }  
  84. }  
  85.   
  86. private bool _isFocused;  
  87.   
  88. public bool IsFocused  
  89. {  
  90.     get { return _isFocused; }  
  91.     set  
  92.     {  
  93.         _isFocused = value;  
  94.         RaisePropertyChanged("IsFocused");  
  95.     }  
  96. }  

Answers (1)