locked
Binding and VisualTreeHelper not working ? RRS feed

  • Question

  • Hi ,

    I'm building a Metro UI c# app.

    I have a bindable listview on my screen with some comboboxes which I bind successfully from my database using VisualTreeHelper.

    Here is some of my code

       private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
                {
                    if (depObj == null)
                    {
                        yield break;
                    }

                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        var child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            yield return (T)child;
                        }

                        foreach (var childOfChild in FindVisualChildren<T>(child))
                        {
                            yield return childOfChild;
                        }
                    }
                }

    private void LoadBindings()

    {

      var db = MyConnection();

                        db.RunInTransaction(() =>
                        {

                            MyCompanyTypes = db.Query<Real_Estate_Manager.App.tbCompanyType>("select * from tbCompanyType");


                        });
                        db.Dispose();

                        foreach (ComboBox tb in FindVisualChildren<ComboBox>(this.itemListView))
                        {

                            if (tb.Name.ToUpper() == "COMPANYTYPES")
                            {
                                MyCompanyTypescb = tb;
                                MyCompanyTypescb.ItemsSource = MyCompanyTypes;
                                break;
                            }
                        }

    }

    My bindings are working fine. I have the code in the loaded event and everything works like a charm.

    The problem starts when I want to REFRESH MYSCREEN.

    So... I have a button which says clear all and I'm  trying to load again the bindings using the visualtreehelper.

    So I went in my button and put the below code

     

    MyCurrentItem = new List<Job.PropertyList>();
                  Job.PropertyList MyTemp = new Job.PropertyList();
                  MyTemp.IsActive = "Y";
                 /MyTemp.AddDate = DateTime.Now;
                  MyTemp.UpdDate = DateTime.Now;
                  MyTemp.Commision = 2;

                  MyCurrentItem.Add(MyTemp);
                  this.itemListView.ItemsSource = null;
                  this.itemListView.Visibility = Windows.UI.Xaml.Visibility.Visible;
                  this.itemListView.ItemsSource = MyCurrentItem;

    LoadBindings(); //The visualtreehelper doesn't work

    The above code , initializes my screen creates a new object and tries to bind again the comboboxes. Without any success.

    My Question is How can I RELOAD the bindings ?????

    Appreciate any help you could give me.

    thank you

    Tuesday, April 23, 2013 9:52 AM

Answers

  • Can you try something along these lines, i.e. waiting for layout update to have occurred before walking the tree:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                // Wait for new layout
                lv.LayoutUpdated += lv_LayoutUpdated;
                // Reset the ItemsSource (example)
                lv.ItemsSource = new List<string>() { "a", "b" };
            }
            void lv_LayoutUpdated(object sender, object e)
            {
                // Cancel any later update
                lv.LayoutUpdated -= lv_LayoutUpdated;
                // Perform VisualTree walk and fill
                // ...
            }


    • Edited by ForInfo Tuesday, April 23, 2013 10:42 AM
    • Marked as answer by zakkar Tuesday, April 23, 2013 10:54 AM
    Tuesday, April 23, 2013 10:41 AM

All replies

  • Can you try something along these lines, i.e. waiting for layout update to have occurred before walking the tree:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                // Wait for new layout
                lv.LayoutUpdated += lv_LayoutUpdated;
                // Reset the ItemsSource (example)
                lv.ItemsSource = new List<string>() { "a", "b" };
            }
            void lv_LayoutUpdated(object sender, object e)
            {
                // Cancel any later update
                lv.LayoutUpdated -= lv_LayoutUpdated;
                // Perform VisualTree walk and fill
                // ...
            }


    • Edited by ForInfo Tuesday, April 23, 2013 10:42 AM
    • Marked as answer by zakkar Tuesday, April 23, 2013 10:54 AM
    Tuesday, April 23, 2013 10:41 AM
  • I will give it a try and revert .

    thank you

    Tuesday, April 23, 2013 10:45 AM
  • THANK YOU. IT WORKED !!!!

    THANK YOU

    Tuesday, April 23, 2013 10:54 AM