Item focusses unexpected

Eduardo Gomez 3,671 Reputation points
2025-03-22T22:50:42.94+00:00

hello, I have a win UI 3 app (Finally I chose, the correct tag lol), where I created a sort of file explorer, but for some reason when I open it

User's image

I do know if we need the code

   <ListView
       Padding="10"
       ItemsSource="{x:Bind FileExplorerWindowViewModel.FolderItems}"
       SelectedItem="{x:Bind FileExplorerWindowViewModel.SelectedFolderItem, Mode=TwoWay}"
       SelectionMode="Single">
       <Interactivity:Interaction.Behaviors>
           <Interactions:EventTriggerBehavior EventName="SelectionChanged">
               <Interactions:InvokeCommandAction Command="{x:Bind FileExplorerWindowViewModel.ItemSeletedCommand}" />
           </Interactions:EventTriggerBehavior>
       </Interactivity:Interaction.Behaviors>
       <ListView.ItemTemplate>
           <DataTemplate x:DataType="models:FolderItem">
               <StackPanel
                   Margin="10"
                   Orientation="Horizontal">
                   <Image
                       Width="32"
                       Height="32"
                       Margin="0,0,10,0"
                       Source="{x:Bind IconPath}" />
                   <TextBlock
                       VerticalAlignment="Center"
                       FontSize="14"
                       FontWeight="Bold"
                       Text="{x:Bind FileName}" />
               </StackPanel>
           </DataTemplate>
       </ListView.ItemTemplate>
   </ListView>


VM

public partial class FileExplorerWindowViewModel : ObservableObject {

    public ObservableCollection<FolderItem> FolderItems { get; set; } = [];

    public ObservableCollection<GcodeItem>? GcodeItems { get; set; } = [];

    [ObservableProperty]
    FolderItem? selectedFolderItem;

    public FileExplorerWindowViewModel() {

        FolderItems = FolderService.GetFolders();
    }

Service

public class FolderService {

    public static ObservableCollection<FolderItem> GetFolders() {

        return [

             new FolderItem(Constants.desktop, Constants.GetImagePath(Constants.Folder)),
             new FolderItem(Constants.documents, Constants.GetImagePath(Constants.Folder)),
             new FolderItem(Constants.downloads, Constants.GetImagePath(Constants.Folder)),
             new FolderItem(Constants.nanoFlowFolder, Constants.GetImagePath(Constants.Folder))
        ];
    }

Demo

https://reccloud.com/u/ebdrz3d

Windows development | Windows App SDK
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2025-03-26T02:27:43.6233333+00:00

    Hi,

    Welcome to Microsoft Q&A!

    According to your description, the current behavior is like the item is set to the focuse state by the keyboard. I couldn't reproduce the issue on my computer. I suggest you could try to use UIElement.Focus(FocusState) Method to set the item's FocusState to unfocused when the listview appears.

    And my system version: Windows 11 Enterprise 22H2 22621.3296

    Thank you

    Jeanine.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.