Search code examples
c#xamluwpmouseeventmousewheel

How to Navigate between xaml controls of UWP using mouse wheel?


I am developing a UWP application in which I have several XAML controls(buttons, textblocks, checkboxes, etc). Ideally, The final user of this app should be able to navigate between these controls ONLY USING MOUSE WHEEL(this is a medical device UI in which only a mouse wheel will be available on top of the monitor). Now my question is that how to force this application use mouse wheel as the primary source of navigation between controls?

Some more feedbacks:

1.Right now, when I run my application in visual studio, I just see mouse pointer and of course buttons are sensitive to mouse clicks but in order to initiate an event, I have to hover to that element and click. MOUSE WHEEL IS NOT WORKING by default to navigate and select controls.

2.when I sideload this UWP application on a raspberry pi device and run the application there, the only way to navigate between controls is using an attached keyboard(possible to navigate and select controls using it). AGAIN ATTACHED MOUSE WHEEL IS NOT WORKING HERE.

  1. an example of controls I use in my code is this:

xaml code:

<Button x:Name="button1" Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />

 <Button x:Name="button2" Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />

 <Button x:Name="button3" Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />

c# code:

private void button1_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
 private void button2_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
 private void button3_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}

in above code, it is not possible to navigate between three buttons using mouse wheel(both in visual studio and raspberry pi).


Solution

  • AGAIN ATTACHED MOUSE WHEEL IS NOT WORKING HERE.

    How did you register the 'MOUSE WHEEL' event in your code? It worked well on my side.

    Please see the following code sample:

    <StackPanel x:Name="root" >
        <Button x:Name="button1"  Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />
    
        <Button x:Name="button2"  Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />
    
        <Button x:Name="button3"  Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />
    </StackPanel>
    
    public MainPage()
    {
        this.InitializeComponent();
        Window.Current.CoreWindow.PointerWheelChanged += CoreWindow_PointerWheelChanged;
    }
    
    private async void CoreWindow_PointerWheelChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.PointerEventArgs args)
    {
        Debug.WriteLine(args.CurrentPoint.Properties.MouseWheelDelta);
        UIElement element;
        if (args.CurrentPoint.Properties.MouseWheelDelta > 0)
        {
            element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Up);
            if (element == null)
            {
                element = FocusManager.FindLastFocusableElement(root) as UIElement;
            }
            var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
            Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
        }
        else
        {
            element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Down);
            if (element == null)
            {
                element = FocusManager.FindFirstFocusableElement(root) as UIElement;
            }
            var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
            Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
        }
    
    }