User avatar
rmcq
Topic Author
Posts: 15
Joined: 18 Feb 2020, 23:31

Focusing a Listbox - Best Practices

03 Mar 2020, 06:28

Hi team.

In my game I have a screen that contains only a Listbox. The expected behaviour is:
- Press A to progress past the screen
- Press B to go back to the previous screen
- Use left/right to scroll through the ListBox items
- Have a model behind each Listbox item that contains an array of data. Use up/down to scroll through those

I have this working for the most part. But I'm having issues with the Listbox focus. I want the it focused by default so we can scroll through the items straight away. Right now it starts deselected and you have to scroll to 'enter' it. Also, when focused, I need to hit B twice to go back to the previous screen. Once to deselect the LB and an extra time to exit the screen.

What's the best way to achieve this? This seems like something that the View should handle but I couldn't find an easy way to add it to the XAML. I'm still new to XAML / WPF so I could be missing something simple. I'm aware that I could use FindName in the code behind but that would couple the View and ViewModel. I have SelectedIndex set and bound in the XAML already.

Any help would be appreciated.

Thanks,
R

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1777
Joined: 22 Dec 2011, 19:20

Re: Focusing a Listbox - Best Practices

05 Mar 2020, 11:02

For selecting the ListBox on load you can use an interactivity trigger like this:
<i:EventTrigger EventName="Loaded">
  <noesis:SetFocusAction TargetName="TheListBox"/>
</i:EventTrigger>
About the to need hit 'B' twice to go back is because of focus engagement being enabled in the ListBox by default. That could be disabled by setting 'noesis:Element.SupportsFocusEngagement' to 'False', unfortunately I found a bug in our code that ignores that property on some scenarios, could you please report a bug in our bugtracker? we will fix it for the next release.
 
User avatar
rmcq
Topic Author
Posts: 15
Joined: 18 Feb 2020, 23:31

Re: Focusing a Listbox - Best Practices

09 Mar 2020, 22:45

Thanks sfernandez. I will give that a go.

I've reported the bug here: https://www.noesisengine.com/bugs/view.php?id=1633
 
User avatar
sfernandez
Site Admin
Posts: 1777
Joined: 22 Dec 2011, 19:20

Re: Focusing a Listbox - Best Practices

12 Mar 2020, 20:48

Thanks for the report.
 
User avatar
rmcq
Topic Author
Posts: 15
Joined: 18 Feb 2020, 23:31

Re: Focusing a Listbox - Best Practices

13 Mar 2020, 05:06

Hey all,

So I've tried what you suggested. It now selects the Listbox on load (yay!) but the B input still isn't working.

Here's the relevant code. At the moment I can progress forward using A button and Enter. I cannot go back with B/Esc. However, if I add KeyBinding Key="Esc" like I have below, I CAN go back with Esc (but not B). Is this the issue you came across with SupportsFocusEngagement?
<Grid
	....
>

	<Viewbox>
		<Grid Width="1920" Height="1080">
			<Grid>
				<Grid>
					<Grid>
						<Viewbox>
							<ListBox x:Name="MatchSettingsGrid"
								ItemsSource="{Binding MatchSettingsList}"
								SelectedItem="{Binding SelectedItem, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
								SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
								ScrollViewer.HorizontalScrollBarVisibility="Disabled"
								ScrollViewer.VerticalScrollBarVisibility="Disabled"
								ItemTemplate="{StaticResource MatchSettingsTemplate}"
								Focusable="True"
								Background="Transparent"
								BorderBrush="Transparent"
								KeyboardNavigation.ControlTabNavigation="None"
								noesis:Element.SupportsFocusEngagement="False"
							>
								<ListBox.ItemsPanel>
									<ItemsPanelTemplate>
										<UniformGrid Rows="1" />
									</ItemsPanelTemplate>
								</ListBox.ItemsPanel>
							</ListBox>
						</Viewbox>
					</Grid>
				</Grid>
			</Grid>
		</Grid>
	</Viewbox>
	<i:Interaction.Triggers>
		<i:EventTrigger EventName="Loaded">
			<noesis:SetFocusAction TargetName="MatchSettingsGrid"/>
		</i:EventTrigger>
		<noesis:GamepadTrigger FiredOn="ButtonDown" Button="Accept">
			<i:InvokeCommandAction Command="{Binding CommandForward}"/>
		</noesis:GamepadTrigger>
		<noesis:GamepadTrigger FiredOn="ButtonDown" Button="Cancel">
			<i:InvokeCommandAction Command="{Binding CommandBack}"/>
		</noesis:GamepadTrigger>
		<noesis:GamepadTrigger FiredOn="ButtonDown" Button="Up">
			<i:InvokeCommandAction Command="{Binding CommandKeyUp}"/>
		</noesis:GamepadTrigger>
		<noesis:GamepadTrigger FiredOn="ButtonDown" Button="Down">
			<i:InvokeCommandAction Command="{Binding CommandKeyDown}"/>
		</noesis:GamepadTrigger>
	</i:Interaction.Triggers>
	<Grid.InputBindings>
		<KeyBinding Key="Esc" Command="{Binding CommandBack}"/>
		<KeyBinding Key="Enter" Command="{Binding CommandForward}"/>
	</Grid.InputBindings>
</Grid>
 
User avatar
sfernandez
Site Admin
Posts: 1777
Joined: 22 Dec 2011, 19:20

Re: Focusing a Listbox - Best Practices

13 Mar 2020, 09:34

Yes, the B key won't work because of the FocusEngagement bug I told you before, we fixed it in our latest release.
 
User avatar
rmcq
Topic Author
Posts: 15
Joined: 18 Feb 2020, 23:31

Re: Focusing a Listbox - Best Practices

14 Mar 2020, 04:50

All good. Wanted to make sure we were talking about the same thing.

Who is online

Users browsing this forum: Silva and 13 guests