0

如何动态地将许多按钮添加到第一个项目的网格和文本框到第二个项目的另一个网格?

不是 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;

}

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

4

1 回答 1

0

以下是如何从 PivotItem 中检索命名元素:

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);
}
于 2013-05-29T18:42:36.167 回答