0

I am using C#, Silverlight, Visual Studio for Windows Phone 7.

I am interested in getting only the visual elements that are currently displayed on the screen during a pivot. For example, my pivot could have 5 PivotItems, but I only want to get the items that are on display after each flick.

Right now, I can get the entire pivot from the visual tree, and obviously I could select a specific PivotItem. But I want to be able to get only the current pivot on the screen. The end goal is to get the absolute position of the elements on the screen, regardless of the current PivotItem.

Is this possible? If so, how?

joulesm
  • 642
  • 1
  • 6
  • 28

1 Answers1

2

SelectedItem and/or SelectedIndex on the Pivot would return the currently visible PivotItem.

Sample Code for you to illustrate my point:

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <!--Pivot Control-->
    <controls:Pivot x:Name="SomePivot" Title="MY SAMPLE PIVOT">
            <StackPanel>
                <Button Content="Button 0"/>
                <Button Content="Button 1"/>
                <Button Content="Button 2"/>
                <Button Content="Button 3"/>
                <Button Content="Button 4"/>
                <Button Content="Button 5"/>
                <Button Content="Button 6"/>
                <Button Content="Button 7"/>
                <Button Content="Button 8"/>
                <Button Content="Button 9"/>
                <Button Content="Button 0"/>
                <Button Content="Button 1"/>
                <Button Content="Button 2"/>
                <Button Content="Button 3"/>
                <Button Content="Button 4"/>
                <Button Content="Button 5"/>
                <Button Content="Button 6"/>
                <Button Content="Button 7"/>
                <Button Content="Button 8"/>
                <Button Content="Button 9"/>
            </StackPanel>
        </controls:PivotItem>
        <controls:PivotItem Header="item2">
            <Button Content="Button D" Name="B"/>
        </controls:PivotItem>
        <controls:PivotItem Header="item3">
            <Button Content="Button D" Name="C"/>
        </controls:PivotItem>
        <controls:PivotItem Header="item4">
            <Button Content="Button D" Name="D"/>
        </controls:PivotItem>
    </controls:Pivot>

    <Button Grid.Row="1" Content="Get Current Pivot Item" Click="GetCurrentPivotItem"/>
</Grid>

Code-Behind:

private void GetCurrentPivotItem(object sender, RoutedEventArgs e)
{
    PivotItem pivotItem = (PivotItem)SomePivot.SelectedItem;
    Debug.WriteLine("Pivot Item : {0}", pivotItem.Header);
    foreach (var element in VisualTreeHelper.FindElementsInHostCoordinates(new Rect(20, 0, 480, 700), pivotItem))
    {
        if (element is Button)
        {
            Debug.WriteLine("{0}", ((Button)element).Content);
        }
    }
}

Does this help?

Gros Lalo
  • 1,078
  • 7
  • 20
  • Thank you!! Yes it does!! The interesting thing is that I am currently triggering my methods through a LayoutUpdated event, which oddly enough is only called a few times. Let's say I have 5 pivots. LayoutUpdated gets triggered during the first 3 flicks, but then never again. Any idea why this is? – joulesm Sep 12 '12 at 08:49
  • I have since changed my code so that this method is called by a PivotSelectionChanged. Still curious why LayoutUpdated is only called a few times before it stops though. – joulesm Sep 12 '12 at 08:55
  • For future reference, it's better to use the LoadedPivotItem event rather than PivotSelectionChanged as then you can get the elements when they are done getting pivoted. – joulesm Sep 12 '12 at 09:19