Domanda

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?

È stato utile?

Soluzione

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?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top