2017-12-28 30 views
1

나는 Pivot.HeaderTemplate에 헤더를 설정 한 Pivot을 가지고 있습니다. 기본적으로 Books의 Names을 보여주는 것입니다. 내 Pivot.ItemTemplate에서 Grid은 내 .xaml.cs에 빌드를 표시하려고하지만 Grid은 내 DataTemplate에 있기 때문에 .xaml.cs의 코드 뒤에 더 이상 Grid x:Name에 액세스 할 수 없습니다. books 내가 뒤에 BooksGrid INY 코드를 액세서 실제로 GridDatatemplate 내의 UWP Grid

를 만들

<Pivot ItemsSource="{x:Bind books}"> 

<Pivot.HeaderTemplate> 
    <DataTemplate x:DataType="local:Book"> 
     <TextBlock Text="{x:Bind Name}"/> 
    </DataTemplate> 
</Pivot.HeaderTemplate> 

<Pivot.ItemTemplate> 
    <DataTemplate> 
     <Grid 
      x:Name="BooksGrid" 
      BorderBrush="Black" BorderThickness="1 1 0 0" 
      Margin="0 10 0 0> 
     </Grid> 
    </DataTemplate> 
</Pivot.ItemTemplate> 

이제 NameTitle

에서 MainPage.xaml이 들어 책의 모음입니다 MainPage.xaml.cs

public MainPage() 
    { 

     this.InitializeComponent(); 
    } 

private void DrawGrid() 
    { 

      //create columns of Grid 
      for (int i = 0; i < booksize.XProperties.Count + 1; i++) 
      { 
       BooksGrid.ColumnDefinitions.Add(new ColumnDefinition 
       { 

       }); 

      } 

      BooksGrid.ColumnDefinitions[0].Width = GridLength.Auto; 
     } 

    .... 

이미 여기 BooksGrid.ColumnDefinitions.Add(...)BooksGrid을 찾을 수 없습니다. 내 정의를 내 DataTemplate에 넣지 않고 내 Pivot 외부에 배치하면 내 DrawGrid이 작동합니다. Grid 내가 솔루션은 즉시 DataTemplate가로드되는대로, 내가 작업 할 Grid instance을 액세서해야한다는 수 있습니다 읽었습니다 내 DataTemplate

내부에있을 때 그래서 MainPage.xaml.cs 그것을 발견하지 않습니다. 그러나 나는 그것을 어떻게 할 지 모른다. 최초의 솔루션에

편집 PART : 그것은 동적으로 생성되기 때문에

나는 또한 당신이 액세스 할 수 없습니다 MainPage.xaml.cs를

private void DrawBooksFront(Front front) 
    { 
     int row; 
     int column; 
     column = booksize.XProperties.IndexOf(front.CustomProps[booksize.XLabel])+1; 
     row = booksize.YProperties.IndexOf(front.CustomProps[booksize.YLabel])+1; 
     Frame newFrame = new Frame(); 
     TaskBoardGrid.Children.Add(newFrame); 
     Grid.SetColumn(newFrame, column); 
     Grid.SetRow(newFrame, row); 


    } 
+0

당신은 DateTemplate을 찾을 수 없습니다해야하며 그것은 나에게 의미가 있지만 나는 또한'공공 무효 DrawBooksFront (전면 앞)'라고 다른 방법으로'BooksGrid'을 사용하고있는 UserControl – lindexi

답변

3

이유는 다른 방법에 BooksGrid을 사용하고 당신의 BooksGrid입니다 books 컬렉션의 각 도서에 대해 따라서 모든 도서에 대해 Grid이 생성됩니다.

옵션 1 :

<Pivot x:Name="Pivot" ItemsSource="{x:Bind books}"> 
    <Pivot.HeaderTemplate> 
     <DataTemplate x:DataType="local:Book"> 
      <TextBlock Text="{x:Bind Name}"/> 
     </DataTemplate> 
    </Pivot.HeaderTemplate> 

    <Pivot.ItemTemplate> 
     <DataTemplate> 
      <Grid 
       BorderBrush="Black" BorderThickness="1,1,0,0" 
       Margin="0,10,0,0" Loaded="DrawGrid"> 
      </Grid> 
     </DataTemplate> 
    </Pivot.ItemTemplate> 

을하고 뒤에 코드 : 당신은 그리드에 Loaded 이벤트를 추가 할 수 있습니다

private void DrawGrid(object sender, RoutedEventArgs e) 
    { 
     Grid grid = sender as Grid; 
     // Load your grid.. 
    } 

편집 - 옵션 2 : 당신이 만약 뒤에있는 코드에서 그리드에 접근하고 싶습니다. (편집시 제안 된 것과 같이) 항상 다음을 할 수 있습니다 :

private void DrawBooksFront(Front front) 
{ 
     // Loop through the pivot's items and get the content from each item's ContentTemplate. 
     foreach (var item in Pivot.Items) 
     { 
      PivotItem pivotItem = Pivot.ContainerFromItem(item) as PivotItem; 
      Grid grid = pivotItem.ContentTemplate.LoadContent() as Grid; 
      // Do something with the grid. 
     } 
} 
+0

사용해야합니다. 거기에'BooksGrid.Children.Add (newFrame)'을 단순히 사용하여'Frame'을 추가하는 것입니다. –

+0

나는 처음 게시물을 편집했습니다. –

+0

@ J.Carden, 제 대답을 편집했습니다. 그게 너를 도와주기를 바란다. :) – Bart

1

당신의 목표는 그리드와 같은 방법으로 [아래 그림]에 PivotItem 내 책의 페이지의 미리보기를 표시하는 경우, 당신은 Pivot.ItemTemplateDataTemplateGridView을 배치하고 데이터를 사용하는 것이 더 낫다 바인딩을 사용하면 해당 페이지를 자동으로 표시하므로 사용자가 보여준 xaml.cs에 코드를 작성할 필요가 없습니다.

우리가 당신을 더 잘 도울 수 있도록, 귀하의 응용 프로그램에 대한 자세한 내용 (주어진 내용과 최종 결과)을 공유하십시오.

enter image description here