Okay, I know this title looks like it has answers all around SO, but none of the answers found here worked for me. So, here goes.
I have this layout:
<Window>
<Grid>
<DockPanel>
<TabControl>
<TabItem>
<Page x:Name="p">
<Grid x:Name="g2">
<TabControl x:Name="tc">
<TabItem x:Name="ti1">
<StackPanel x:Name="sp">
<C:TextBox x:Name="txt"/>
</StackPanel>
</TabItem>
<TabItem x:Name="ti2">
<C:DataGrid x:Name="dg"/>
</TabItem>
</TabControl>
</Grid>
</Page>
</TabItem>
</TabControl>
</DockPanel>
</Grid>
</Window>
Now, my goal is to put focus on txt TextBox when ti1 TabItem gets selected and on dg DataGrid when ti2 TabItem gets selected. Also, I would really love to set this in XAML.
Note: I can only use controls that are named here, so up until Page control.
What have I tried so far:
- setting
FocusManager.FocusedElement="{Binding ElementName=txt}"on all of the parent controls in the parent tree of thetxtControl(up untilPage). - setting
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"on thetxtanddgcontrols. setting focus in code through
TabControl'sSelectionChangedevent:- if ( ti1.IsSelected ) { tc.UpdateLayout(); FocusManager.SetFocusedElement( sp, txt ); }
- and
- if ( ti1.IsSelected ) { tc.UpdateLayout(); txt.Focus(); }
TextBox and DataGrid controls are created like UserControls, but are actually classes that inherit TextBox and DataGrid, like this:
<TextBox ... </TextBox>
and
public partial class TextBox : System.Windows.Controls.TextBox
As I said, XAML solution is desired, but I will also settle for a code one, if former is not possible.