0

In my code,

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString))
        {
            ctx.CreateIfNotExists();
            ctx.LogDebug = true;
            var buses = from c in ctx.Bus_routes
                             select new bus_list{ BUS_NO = c.BUS_NO, SOURCE = c.SOURCE, DESTINATION = c.DESTINATION};
            busno_list.ItemsSource = buses.ToList();
        }
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string temp;
        TextBlock nameBox;
        ListBoxItem currentSelectedListBoxItem;
        for(int i=0;i<busno_list.Items.Count;i++)
        {
            currentSelectedListBoxItem = this.busno_list.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;

            nameBox = FindDescendant<TextBlock>(currentSelectedListBoxItem);

            temp = nameBox.Text;
            if (temp.Contains(searchbox.Text))
            {
                busno_list.SelectedIndex = i;
                busno_list.ScrollIntoView(busno_list.SelectedItem);
                return;
            }
        }
    }

and in FindDescendant function ,

    private T FindDescendant<T>(DependencyObject obj) where T : DependencyObject
    {
        // Check if this object is the specified type
        if (obj is T)
            return obj as T;

        // Check for children
        int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
        if (childrenCount < 1)
            return null;

        // First check all the children
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child is T)
                return child as T;
        }

        // Then check the childrens children
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = FindDescendant<T>(VisualTreeHelper.GetChild(obj, i));
            if (child != null && child is T)
                return child as T;
        }

        return null;
    }

But at the line
int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
debugger is always throwing InvalidOperationException (Reference is not a valid visual DependencyObject) and in Autos watch tool, obj is showing null value, even if there are already 557 rows in listbox.

So I am not able to correct this exception.
And i find this method best for searching through the listbox having datatemplate as given on How to access a specific item in a Listbox with DataTemplate?. Please suggest where i am going wrong or if there is a better alternative.

Community
  • 1
  • 1
yatendra
  • 317
  • 4
  • 11

1 Answers1

1

Not sure why you're trying to do this by finding the generated element, when you have the source, i.e. something like:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString))
    {
        ctx.CreateIfNotExists();
        ctx.LogDebug = true;
        var buses = from c in ctx.Bus_routes
                         select new bus_list{ BUS_NO = c.BUS_NO, SOURCE = c.SOURCE, DESTINATION = c.DESTINATION};
        busno_list.ItemsSource = buses.ToList();

        searchbox.Text = buses[20].SOURCE; // or whatever
    }
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • sir my motive is not to access a particular record, but what i am trying to accomplish here is that whenever the user is entering the substring in searchbox, it should scroll to the required row. I am editing the question, please take a one more look at it. – yatendra Sep 08 '13 at 21:25
  • You might find this easier to do by adopting the MVVM pattern - i.e. Have a class that represents the data behind your search screen (i.e. the ViewModel), that has the collection of applicable bus routes, a property for the search term, and a property for the selected index - you should them be able to bind them easily enough in the XAML without needing to hack this in the code behind of the View – Rowland Shaw Sep 09 '13 at 07:46