Have an ItemsControl in my View, that is bound to an ObservableCollection from ViewModel. The collection is filled, and afterwards an event from VM to view is raised (think search results and SearchFinished event). how to move keyboard focus to the first item in an ItemsControl? I'm using MVVM pattern
Asked
Active
Viewed 719 times
1
-
Maybe you should try LayoutUpdated event of ItemsControl, in its handler just find its first child and use Keyboard.Focus(child) – Den Jul 30 '14 at 12:28
-
How to create LayoutUpdated event in viewmodel? – user3729898 Jul 30 '14 at 12:42
-
You create it in code behind of your View, its ok and doesn't violate MVVM pattern because its view specific behavior that you want – Den Jul 30 '14 at 12:44
-
You may be able to use the [ItemsControl.ItemContainerGenerator.Status](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.statuschanged(v=vs.110).aspx) to fire your focus event when [GeneratorStatus](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.generatorstatus(v=vs.110).aspx) == ContainersGenerated – Rachel Jul 30 '14 at 14:36
-
Can you please give me a sample code for ItemsControl.ItemContainerGenerator.Status ? – user3729898 Jul 31 '14 at 10:36
-
Does this answer your question? [WPF ItemsControl - how to know when the items finished loading, so that I can focus the first one?](https://stackoverflow.com/questions/2492214/wpf-itemscontrol-how-to-know-when-the-items-finished-loading-so-that-i-can-fo) – StayOnTarget Mar 08 '21 at 16:57
2 Answers
0
I've done this before using a behavior:
public sealed class FocusBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
AssociatedObject.Focus();
}
}
This is then used in XAML as follows:
<TextBox x:Name="UsernameTextBox"
Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
KeyboardNavigation.TabIndex="0">
<i:Interaction.Behaviors>
<b:FocusBehavior />
</i:Interaction.Behaviors>
</TextBox>
You will need a reference to the System.Windows.Interactivity assembly to pull in the Interaction namespace.

AwkwardCoder
- 24,893
- 27
- 82
- 152
0
I found the solution for this..
private void VerifiValue_Loaded(object sender, RoutedEventArgs e)
{
if (verificationIC.Items.Count > 0)
{
UIElement uiElement = (UIElement)verificationIC.ItemContainerGenerator.ContainerFromIndex(0);
TextBox t = GetChild<TextBox>(uiElement);
t.Focus();
t.SelectAll();
}
}
public T GetChild<T>(DependencyObject obj) where T : DependencyObject
{
DependencyObject child = null;
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child.GetType() == typeof(T))
{
break;
}
else if (child != null)
{
child = GetChild<T>(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
}
}
return child as T;
}

user3729898
- 11
- 3