Comments: ** Comment from web user: eilefh **
JobaDiniz' method isn't very useful in a MVVM scenario, however you can subclass the BusyIndicator class and add a dependency property for the element for which to focus like this:
public class MyBusyIndicator : BusyIndicator
{
public UIElement SetFocusTo
{
get { return (UIElement)GetValue(SetFocusToProperty); }
set { SetValue(SetFocusToProperty, value); }
}
public static readonly DependencyProperty SetFocusToProperty =
DependencyProperty.Register("SetFocusTo", typeof(UIElement), typeof(MyBusyIndicator), new PropertyMetadata(null));
public MyBusyIndicator()
: base()
{
}
protected override void OnIsBusyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
{
base.OnIsBusyChanged(e);
var element = this.SetFocusTo;
if (element == null)
{
return;
}
element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() => element.Focus()));
}
}
and in XAML:
<v:MyBusyIndicator IsBusy="{Binding IsBusy}" SetFocusTo="{Binding ElementName=searchText}">
<TextBox x:Name="searchText" />
</v:MyBusyIndicator>