locked
ContainerFromIndex is Nothing after setting ItemsSource

    Question

  • I have an ItemsControl in which I sometimes want to set focus to a TextBox in the ItemTemplate after setting the ItemsSource. Here is the code I am currently trying to use:

    System.Diagnostics.Debug.WriteLine("Checkpoint 1: {0}", Me.ContainerFromIndex(0) Is Nothing)
    Me.ItemsSource = Me.PlayerNames.Concat({""}).Select(Function(n, i) New With {.PlayerName = n, .Index = i})
    System.Diagnostics.Debug.WriteLine("Checkpoint 2: {0}", Me.ContainerFromIndex(0) Is Nothing)
    

    When I run this code, I get the output of

    False
    True

    Which means that Me.ContainerFromIndex(0) is not Nothing before setting the ItemsSource, but it is Nothing afterwards. Is this because the binding has not yet happened? What can I do to set focus to a TextBox in the ItemTemplate for a specific item? Thanks!


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, October 14, 2014 5:01 PM

All replies

  • Hi Nathan,

    What can I do to set focus to a TextBox in the ItemTemplate for a specific item?

    -> try to find the current textbox once the binding is finished, you could ref to the article: Finding
    Element in ItemsControl DataTemplate (ListView/GridView/FlipView) - Window Store
    Apps
    , after you have found the correct textbox, set focus on it.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 15, 2014 2:15 AM
    Moderator
  • "try to find the current textbox once the binding is finished"

    That is where the problem is: When is it finished?

    What I am doing is obtaining an index based on the current information, then adding an item to my List, and then binding by setting ItemsSource. When is the binding finished so that I can use ContainerFromIndex? Also, I was unable to use your link, so you may want to try sending that again if you think it will help. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 15, 2014 3:45 PM
  • Hi Nathan,

    What event are you using to run your code? The Loaded event would be good as it is lauched after binding is completed.

    Leszek


    My Apps


    • Edited by ata6502 Wednesday, October 15, 2014 6:10 PM
    Wednesday, October 15, 2014 6:09 PM
  • The code is being run in the TextChanged event of the TextBox in the ItemTemplate. I run the code in this event because I want to modify certain items in the ItemsControl depending on whether the TextBox is empty, whether it is the last one, etc. I am able to do all of these things fine, but then the binding causes the TextBox to lose focus (since it is actually an new TextBox created during binding), which is why I need to access the TextBox (so that I can set focus to it, otherwise the user will need to reselect the TextBox every time they type a letter). Which control's Loaded event are you referring to? Don't the Loaded events only occur once for each control? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 15, 2014 8:01 PM
  • Thanks Nathan for clarification.

    I had a similar problem in a WPF application. Back then, a solution was to create an event handler for the ItemContainerGenerator.StatusChanged event. In this event handler I used to check the Status of the generator: generator.Status == GeneratorStatus.ContainersGenerated. This way I could determine if binding was completed. If so, I could simply find a TextBox in a ListBox by traversing the visual tree and set the focus.

    I have tried to create a counterpart code in Windows Store XAML and I could not find corresponding functionality. Moreover, it seems Microsoft depreciated ItemContainerGenerator in Windows 8.1 :(  I think the key is to find out how to handle the StatusChanged event in Windows 8.1 store apps. I haven't figure it out yet.

    Leszek


    My Apps


    • Edited by ata6502 Thursday, October 16, 2014 1:32 AM
    Thursday, October 16, 2014 1:31 AM
  • Sorry about the link, it should be: http://social.technet.microsoft.com/wiki/contents/articles/17502.finding-element-in-itemscontrol-datatemplate-listviewgridviewflipview-window-store-apps.aspx (validated)

    I don't know when Binding will finish, but as I know the UI should update the layout once the new items binding to the Control, and then you could try to find the Item which you want to focus from the ItemsTemplate and set the focus on it.

    Instead of using ContainerFromIndex, I think you would like to read the article I suggested to find the correct control from your ItemTemplate.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, October 27, 2014 8:49 AM
    Moderator