开发者

how to access a ListBox inside a Pivot in windows phone 7?

i have a a Pivot that has a ListBox defined as its Pivot.ItemTemplate as the following.

<controls:Pivot x:Name="pivot">
 <controls:Pivot.ItemTemplate>
  <DataTemplate>
   <ListBox x:Name="listBox">
   ...
   </ListBox>
  </DataTemplate>
 </controls:Pivot.ItemTemplate>
</controls:Pivot>

how do i programmatically access the corresponding ListBox control corresponding to the Pivot.SelectedItem or Pivot.SelectedIndex?

i tried something similar to this link http://www.windowsphonegeek.com/tips/how-to-access-a-control-placed-inside-listbox-itemtemplate-in-wp7.

var count = VisualTreeHelper.GetChildrenCount(pivotItem);
for(int i=0; i < count; i++) {
 var child = VisualTreeHelper.GetChild(pivotItem, i);
 if(child is ListBox) {
  //do something
 } else {
  Debug.WriteLine(child.GetType());
 }
}

for some reason, i get System.Windows.Controls.Grid on the Debug.WriteLine.

the reason why i need to get a handle or access the ListBox inside the Pivot (that is currently on display/开发者_如何转开发selected), is because i need to reset its view (scroll it back to the top). the ListBox is data bound to ObservableCollection, and when i update the collection, the scroll position needs to be placed back to the top; otherwise, everything works (data binding/visual display), except now the view is stuck in the middle or where the user currently is. if there's an easier way to do this without getting a handle on the ListBox, i'm open to that solution as well.

just in case anyone is interested, i tinkered and came up with something that works specifically for my case. the code is below. basically, i had to get the PivotItem first.

PivotItem pivotItem = pivot.ItemContainerGenerator.ContainerFromItem(myObject) as PivotItem; 

i then created a local variable to store the ListBox (if it's found) and recursed the tree view model.

ListBox listBox = null;
Recurse(pivotItem, ref listBox);

and my Recurse function looks like the following.

private void Recurse(DependencyObject obj, ref ListBox listBox) { 
 if(obj is ListBox) {
  listBox = obj as ListBox;
  return;
 }

 var count = VisualTreeHelper.GetChildrenCount(obj);
 for(int i=0; i < count; i++) {
  var child = VisualTreeHelper.GetChild(obj, i);
  Recurse(child, ref listBox);
 }
}


try:

(Listbox)VisualTreeHelper.GetChild((pivot.SelectedItem as PivotItem), 0); 


Looks like this was a while back, but this is what worked for me:

First get the PivotItem:

PivotItem pivotItem = Pivot.ItemContainerGenerator.ContainerFromItem(Pivot.SelectedItem) as PivotItem;

Then get the first child, a ListBox, from the PivotItem:

        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++) {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T) {
                return (T)child;
            } else {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }

Then call:

ListBox listBox = FindFirstElementInVisualTree<ListBox>(pivotItem);


use StackPanel inside your ListBox this link may help you http://blogs.msdn.com/b/oren/archive/2010/11/08/wp7-silverlight-perf-demo-1-virtualizingstackpanel-vs-stackpanel-as-a-listbox-itemspanel.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜