简体   繁体   中英

Access items in listbox in windows phone 7?

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.

Not sure why you're trying to do this by finding the generated element, when you have the source, ie 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM