How to focus with keyboard when Wpf ListView is empty?

fatih uyanık 80 Reputation points
2023-05-08T08:39:32.91+00:00

Hello

When a Listview control is full in a wpf project, when I press the tab key with the keyboard, it can get keyboard focus. But the keyboard is not focused when Listview is empty. How can I get the ListView to focus when navigating with the tab key even when it's empty?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,694 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,480 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 46,961 Reputation points Microsoft Vendor
    2023-05-08T09:56:53.1066667+00:00

    Hi,@fatih uyanık. Welcome Microsoft Q&A.

    By default, WPF controls like ListView do not receive focus when they are empty. This behavior is intentional and helps improve the user experience by avoiding confusion and ensuring that the user only interacts with controls that are currently usable.

    You could use the following code to override this behavior and allow the ListView to receive focus when it is empty.

    <Window ...
           PreviewKeyDown="ListView_PreviewKeyDown"
           >
        <StackPanel>
            <TextBox Text="hello" Height="100" Width="100" Background="Aquamarine"/>
    
            <ListView Name="lv" Focusable="True" Height="100" >
                <ListView.Style>
                    <Style TargetType="ListView">
                        <Style.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.Style>
            </ListView>
    
        </StackPanel>
    </Window>
    
    

    codebedhind:

      private void ListView_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Tab)
                {
    
                    lv.Focus();
                    e.Handled = true;
                }
            }
    
    

    The result:

    User's image


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. fatih uyanık 80 Reputation points
    2023-05-08T10:17:52.4666667+00:00

    Hello

    Didn't quite get what I wanted. My window has three buttons and a ListView. ListView doesn't get focus when empty when navigating from buttons with tab. i.e. I wanted the buttons to focus while the ListView was moving towards it. How can I do that?

    Thanks.


  3. fatih uyanık 80 Reputation points
    2023-05-09T06:56:14.9333333+00:00

    Hello

    Is there any solution about this? I have not yet achieved the desired result.

    0 comments No comments

  4. don bradman 621 Reputation points
    2023-05-15T05:18:17.11+00:00

    To get the ListView to focus when navigating with the tab key in a WPF project, even when it is empty, you can set the IsTabStop property of the ListView to true, and set the TabIndex property of each control to ensure that the buttons and the ListView can both be in focus.

    Here is an example:

    <Button TabIndex="1" />
    <Button TabIndex="2" />
    <Button TabIndex="3" />
    <ListView IsTabStop="True" TabIndex="4" />
    

    Hope this is what you're looking for.

    0 comments No comments