0

On button click a GalaSoft.MvvmLight.Messaging message is triggered across all viewModels that have the message receiver registered. A new list of data is acquired asynchronously and used to update the UI. The UI updates but when I call the following

var contentControls = VisualTreeQueryHelper.FindChildrenOfType(this).ToList();

It still reflects the content count and indices of the old list even though the visual list on screen is correct. This breaks my focus logic that uses index and list size to set focus.

If I navigate away from the view and come back then the contentControls list is corrected.

private IList<CoreViewModel> itemViewModels;
public IList<CoreViewModel> ItemViewModels
{
    get { return this.itemViewModels; }
    set
    {
        this.itemViewModels = value;
        RaisePropertyChanged("ItemViewModels");
    }
}

 public static IEnumerable<T> FindChildrenOfType<T>(DependencyObject objectInParentTreeToStartFrom)
    where T : DependencyObject
{
    List<T> list = new List<T>();

    FindChildrenOfTypeImpl<T>(objectInParentTreeToStartFrom, list);

    return list;
}

private static void FindChildrenOfTypeImpl<T>(DependencyObject objectInParentTreeToStartFrom, List<T> list)
    where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(objectInParentTreeToStartFrom); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(objectInParentTreeToStartFrom, i);

        if (child is T)
        {
            list.Add(child as T);
        }
        else
        {
            FindChildrenOfTypeImpl(child, list);    
        }
    }
}
Fabii
  • 3,820
  • 14
  • 51
  • 92
  • Since this runs asynchronously, could it be that `var contentControls = VisualTreeQueryHelper.FindChildrenOfType(this).ToList();` runs after the list is updated? – Domysee Mar 03 '16 at 17:15
  • @Domysee Its runs after the list is created, which is what I want. It would then retrieve the update visual tree. I guess my question is how to forcibly update the binding and visual tree. – Fabii Mar 03 '16 at 17:21

0 Answers0