Setting Focus Directly to Expander Button
Posted: 30 Sep 2024, 19:22
I am trying to set focus to the `ToggleButton` of an `Expander` element when the user control gains focus. Is there a way to reference the internal element of the Expander in the target field of SetFocusAction? Is there any other way of setting focus like this through XAML?
The goal of this is to have the focus set to the button as soon as the screen becomes visible so the user can click it without needing to navigate first.
At runtime, the hierarchy looks something like this:
> Expander (Focus goes here)
>> Dock Panel
>>>ToggleButton (Want focus to go here)
It would be nice if there was the option to do something like the line below but I haven't found any example doing something similar yet:
Any help or ideas are much appreciated!
The goal of this is to have the focus set to the button as soon as the screen becomes visible so the user can click it without needing to navigate first.
Code: Select all
<Grid x:Name="StateTriggers">
<b:Interaction.Triggers>
<b:PropertyChangedTrigger Binding="{Binding IsVisible, ElementName=MyContainer}">
<noesis:SetFocusAction TargetName="MyExpander"/>
</b:PropertyChangedTrigger>
</b:Interaction.Triggers>
</Grid>
<Grid x:Name="MyContainer" Width="400">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<ScrollViewer x:Name="Content" Grid.Row="0" Margin="20,20,10,20" VerticalAlignment="Top" VerticalScrollBarVisibility="Visible">
<StackPanel>
<Expander x:Name="MyExpander" Template="{StaticResource MyExpanderTemplate}" OverridesDefaultStyle="True" IsExpanded="True" DataContext="{Binding MyExpanderModel}" KeyboardNavigation.DirectionalNavigation="Continue">
<Expander.Header>
<TextBlock Text="This is an expander"/>
</Expander.Header>
<ItemsControl x:Name="MyExpanderItems" ItemTemplate="{StaticResource MyExpanderItemTemplate}" ItemsSource="{Binding Items}" Margin="0,10,0,0" Focusable="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Expander>
</StackPanel>
</ScrollViewer>
</Grid>> Expander (Focus goes here)
>> Dock Panel
>>>ToggleButton (Want focus to go here)
It would be nice if there was the option to do something like the line below but I haven't found any example doing something similar yet:
Code: Select all
<noesis:SetFocusAction TargetName="MyExpander.DockPanel.ToggleButton"/>