从后面的代码动态添加按钮或图像到pivotitem

时间:2013-05-29 12:15:05

标签: windows-phone-7 pivot pivotitem

如何动态地为第一个项目和第二个项目中的另一个网格添加多个Button到网格?

不是XAML

我找不到名称GridItem - 后面的代码中不存在名称:( 我试图找到Visual Tree Helper :(

PivotItem pivotVHT = (PivotItem)mainSecondPivot.SelectedItem;

    foreach (var element in VisualTreeHelper.FindElementsInHostCoordinates(new Rect(20, 0, 480, 700), pivotVHT))
    {
        if (element is TextBlock)
        {
            Debug.WriteLine("{0}", ((TextBlock)element).Text);

            TextBlock test = ((TextBlock)element);
            test.Text = "TEST";
                        }

    }

VisualTreeHelper只更改文本mainFirstPivot,可视化树帮助器看不到mainSecondPivot

XAML:

    <controls:Pivot Title="Photo Gallery" Name="mainSecondPivot" >
            <controls:Pivot.HeaderTemplate>
                <DataTemplate>
                   <Grid
                          x:Name="PivitGrid"
                        >
                          <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="1*"/>
                          </Grid.ColumnDefinitions>
                          <Grid.RowDefinitions>
                          <RowDefinition Height="1*"/>
                          </Grid.RowDefinitions>
                   <Image
                         Name="PivotImageGalery"
                         Source="{Binding imgSrc}"
                         >

                   </Image>
                   <TextBlock 
                         x:Name="TextBlockPivot"
                         Text="{Binding textBlockPivotName}" 
                     >


                   </TextBlock> 
               </Grid>
               </DataTemplate>
            </controls:Pivot.HeaderTemplate>
            <controls:Pivot.ItemTemplate>
                <DataTemplate>
    <ScrollViewer
               Name="SVName"
              Width="Auto"
              VerticalScrollBarVisibility ="Hidden"
              HorizontalScrollBarVisibility="Disabled"
                    >
                    <Grid
                     x:Name="GridItem"
                      >
                          **HERE**

                    </Grid>
</ScrollViewer>
                </DataTemplate>
            </controls:Pivot.ItemTemplate>
    </controls:Pivot>

C#

public static class SelectedIndex
{
    public static int SelectedIndexInt = 0;// OR SOME NUMBER  
}

 public class IListPivot
        {

                public ImageSource imgSrc { get; set; }
                public String textBlockPivotName { get; set; }

            }

            public secondPage()
       {
                    InitializeComponent();


                    IList<IListPivot> PivotList = new List<IListPivot>();


        for (z = 0; z <= 7; z++)
          {
                 PivotList.Add(new IListPivot()
             { 
                 imgSrc = new System.Windows.Media.Imaging.BitmapImage(new Uri("URI", UriKind.Relative)),
                 textBlockPivotName = "TEXT" 
             });  
          }
            mainSecondPivot.ItemsSource = PivotList;

            mainSecondPivot.Loaded += new RoutedEventHandler (PivotLoaded);
            mainSecondPivot.SelectedIndex = SelectedIndex.SelectedIndexInt
      }

                public void PivotLoaded(object sender, EventArgs e)


          {
                     PivotItem pivotItemVHT = (PivotItem)mainSecondPivot.ItemContainerGenerator.ContainerFromIndex(SelectedIndex.SelectedIndexInt);

                    var root = VisualTreeHelper.GetChild(((VisualTreeHelper.GetChild(pivotItemVHT, 0) as Grid).Children[0] as ContentPresenter), 0) as FrameworkElement;
                    Debug.WriteLine(" root " + root);
                    Debug.WriteLine(" root Name " + root.Name);
                    ScrollViewer scr = (ScrollViewer)root;
                    TextBox BoxText1 = new TextBox();
                    BoxText1.Text = a.ToString();
                    scr.Content = BoxText1;

}

仅添加到每个项目中的一个 帮助

1 个答案:

答案 0 :(得分:0)

以下是如何从数据透视表中检索命名元素:

public FrameworkElement GetPivotElement(int pivotIndex, string name)
{
    var pivot = mainSecondPivot.ItemContainerGenerator.ContainerFromIndex(pivotIndex);
    var root = VisualTreeHelper.GetChild(((VisualTreeHelper.GetChild(pivot, 0) as Grid).Children[0] as ContentPresenter), 0) as FrameworkElement;
    return root.FindName(name);
}