When a listview has focus, the default behaviour of an enter key press is to pick the first element of the listview, Up and down arrow keys scrolls the listview. I am trying to prevent this default behaviour and hook up my custom logic.
I am able to implement Access keys using KeyDown for a listview as follows:
Code behind approach:
CoreWindow.GetForCurrentThread().KeyDown += KeyDownHandler;
MVVM approach:
<ListView SelectedIndex="{Binding IsSelected, Mode=TwoWay}"/>
Triggering the Keydown property:
<core:EventTriggerBehavior EventName="KeyDown">
<core:InvokeCommandAction Command="{x:Bind VMDataContext.KeyDownCommand}" />
</core:EventTriggerBehavior>
And used behaviours to scroll the scrollbar of the listview to the selected index:
<corebehaviors:ListViewScrollBehaviour SelectedIndex="{x:Bind IsSelected, Mode=OneWay}"/>
The above handlers are getting triggered when the listview doesn't have focus. When the listview has focus, the default behaviour of arrow up, down and Enter key is getting triggered and not my attached behaviour. Is there a way to prevent the default behaviour?
Consider extending the ListView control and overriding the OnKeyDown handler.
public class ExtendedListView : ListView
{
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter || e.Key == VirtualKey.Up || e.Key == VirtualKey.Down)
{
return;
}
base.OnKeyDown(e);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With