none
[UWP] I cant set focus on a text box in ListViewItem RRS feed

  • Question

  • Hello,

    I have, a ListView

                        <ListView x:Name="lvProjects" x:Uid="lvProjects" Margin="15,10,20,0" Foreground="Blue"
                                IsItemClickEnabled="True"
                                ItemClick="lvProjects_ItemClick"
                            >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="30"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <TextBox x:Name="tbxProjectName" Text="{Binding showedName, UpdateSourceTrigger=Explicit, Mode=TwoWay}"
                                                                                             
                                             IsEnabled="{Binding isEdited}"
                                                
                                             Grid.Column="0" HorizontalAlignment ="Stretch" BorderThickness="0" Margin="{Binding itemTypeMargin}"
                                                 Background="{Binding itemTypeBackground}"
                                             KeyDown="tbxProjectName_KeyDown" 
                                             GotFocus="tbx_GotFocus"                                             
                                             >
                                        </TextBox>
                                        <Button x:Name="btnProjectMenuButton" Content=".." Grid.Column="1" HorizontalAlignment="Right" Margin="5,0,0,0"                                           
                                                Click="ProjectMenuButton_Click"    
                                                FlyoutBase.AttachedFlyout="{Binding itemType, Converter={StaticResource converter}}"/>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

    This ListView has a TextBox and a Button in every item.

    If I click the button Flayout Menu is showed. On menu is placed a command "Rename".

    Where I click this command I want to set focus on a TextBox.

    Clicking Rename is handled by a handler showed below:

            private async void mfiProject_RenameProject(object sender, RoutedEventArgs e)
            {
                MenuFlyoutItem mfi = sender as MenuFlyoutItem;
                var dataItem = mfi.DataContext;
                ListViewItem lvi = lvProjects.ContainerFromItem(dataItem) as ListViewItem;
                int index = lvProjects.IndexFromContainer(lvi);
                PFM.projectNamesList[index].isEdited = true;

                //Windows.UI.Popups.MessageDialog errMsg = new Windows.UI.Popups.MessageDialog(lvi.ToString());
                //await errMsg.ShowAsync();

                //Get ItemContainer
                DependencyObject dob = VisualTreeHelper.GetChild(lvi, 0);
                //Get DataContext StackPanel or Grid
                dob = VisualTreeHelper.GetChild(dob, 0);
                //Get first TextBox in ListViewItem
                dob = VisualTreeHelper.GetChild(dob, 0);
                //SetFocus
                TextBox tbx = (dob as TextBox);
                tbx.Focus(FocusState.Programmatic);
            }

    This code finds right text box but for any reason it cant set focus on it.

    More, if I uncomment in tis handler errMessage code, Message is showed, and after user acceptance, TextBox is focused properly.

    What is the reason that TextBox focus cant be set, How to Fix it?

    Tuesday, November 29, 2016 9:07 PM

Answers

  • You should be able to use the dispatcher to call the Focus method for the TextBox to get focused as expected:

    private async void mfiProject_RenameProject(object sender, RoutedEventArgs e)
            {
                MenuFlyoutItem mfi = sender as MenuFlyoutItem;
                var dataItem = mfi.DataContext;
                ListViewItem lvi = lvProjects.ContainerFromItem(dataItem) as ListViewItem;
    
                int index = lvProjects.IndexFromContainer(lvi);
                DependencyObject dob = VisualTreeHelper.GetChild(lvi, 0);
                dob = VisualTreeHelper.GetChild(dob, 0);
                dob = VisualTreeHelper.GetChild(dob, 0);
                TextBox tbx = (dob as TextBox);
    
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow
                    .Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
                () => tbx.Focus(FocusState.Programmatic));
                
            }
    


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by BitSmithy Tuesday, December 6, 2016 9:19 PM
    Sunday, December 4, 2016 1:35 PM

All replies

  • I found a probably reason, but I don't know how to fix it.

    If I click the command on a FlayoutMenu opened by clicking a button, the Menu closes and focus is set on a button.

    Tuesday, November 29, 2016 9:21 PM
  • Hi BitSmithy,

    In the document of the FlyoutBase.AttachedFlyout, it said: To attach a flyout to a Button, use Button.Flyout instead.

    When I click the button in the Button.Flyout and set the focus to the TextBox in the event that it can get focus.

    Also would you please share us a simple reproduced project, it will help us analyze and diagnose this issue more clearly.


    Best Regards,

    Jayden Gu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Jayden Gu Wednesday, November 30, 2016 6:34 AM format
    Wednesday, November 30, 2016 6:34 AM
  • I changed my code from FlyoutBase.AttachedFlyout, to Button.Flayout.

    But my problem isn't solved.

    I tested more improved code using FocusManager  but I cant fix the problem.

    How to share code?

    Wednesday, November 30, 2016 10:20 PM
  • Hi BitSmithy,

    As you do not post the complete code that I can not see your issue. You can upload your project to the GitHub. Also you can zip the project and upload to the onedrive.


    Best Regards,

    Jayden Gu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 1, 2016 2:58 AM
  • Here is the link to OneDrive with code files:

    https://1drv.ms/f/s!AtYbJ9kyapZFgtQ8Z5B1xvnUwsNu8w

    Friday, December 2, 2016 10:07 PM
  • You should be able to use the dispatcher to call the Focus method for the TextBox to get focused as expected:

    private async void mfiProject_RenameProject(object sender, RoutedEventArgs e)
            {
                MenuFlyoutItem mfi = sender as MenuFlyoutItem;
                var dataItem = mfi.DataContext;
                ListViewItem lvi = lvProjects.ContainerFromItem(dataItem) as ListViewItem;
    
                int index = lvProjects.IndexFromContainer(lvi);
                DependencyObject dob = VisualTreeHelper.GetChild(lvi, 0);
                dob = VisualTreeHelper.GetChild(dob, 0);
                dob = VisualTreeHelper.GetChild(dob, 0);
                TextBox tbx = (dob as TextBox);
    
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow
                    .Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
                () => tbx.Focus(FocusState.Programmatic));
                
            }
    


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by BitSmithy Tuesday, December 6, 2016 9:19 PM
    Sunday, December 4, 2016 1:35 PM