简体   繁体   English

Datatemplate内部的UWP网格

[英]UWP Grid inside Datatemplate

I have a Pivot where I set the header in my Pivot.HeaderTemplate it is basically just showing Names of Books. 我有一个Pivot ,我设置的头在我Pivot.HeaderTemplate它基本上只是显示Names图书。 In my Pivot.ItemTemplate I want to show a Grid which is build in my .xaml.cs but since the Grid is in my DataTemplate I can not access the Grid x:Name anymore in the code behind in .xaml.cs. 在我的Pivot.ItemTemplate我想显示一个在.xaml.cs中构建的Grid ,但是由于该Grid在我的DataTemplate我无法再在.xaml.cs中的代码中访问Grid x:Name books is a Collection of Books which contains a Name and a Title booksbooks的集合,其中包含NameTitle

MainPage.xaml MainPage.xaml

<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>

Now I want to acces BooksGrid iny the code behind and actually create the Grid 现在我想进入电影BooksGrid背后INY代码,实际上创建Grid

MainPage.xaml.cs 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;
        }  

     ....

Already here at BooksGrid.ColumnDefinitions.Add(...) I get the error that BooksGrid can not be found. 我已经在这里的BooksGrid.ColumnDefinitions.Add(...)收到错误消息, BooksGrid My DrawGrid works if I do not place the Grid definition in my DataTemplate and also outside my Pivot . 如果我没有将Grid定义放置在DataTemplate ,也未将其放置在Pivot之外,则DrawGrid起作用。 So the MainPage.xaml.cs does not find it when the Grid is inside my DataTemplate 因此,当Grid位于我的DataTemplate内部时, MainPage.xaml.cs找不到它

I've read that the solution might be that I have to acces the Grid instance that I want to work with, as soon as the DataTemplate gets loaded. 我已经读过解决方案可能是一旦加载DataTemplate ,我就必须访问要使用的Grid instance But I do not know how to do that either. 但是我也不知道该怎么做。

EDIT PART to first solution: 第一个解决方案的编辑部分:

I'm also using BooksGrid in another method MainPage.xaml.cs 我还在其他方法MainPage.xaml.cs中使用BooksGrid

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);


    }

The reason you cannot access your BooksGrid is because it will be dynamically generated for each book in the books collection. 之所以无法访问BooksGrid是因为它将为books集合中的每本书动态生成。 So for every book a Grid will be generated. 因此,将为每本书生成一个Grid

OPTION 1: You can add a Loaded event to your grid: 选项1:您可以将Loaded事件添加到网格中:

<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>

and in your code behind: 并在您的代码后面:

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

EDIT - OPTION 2: If you'd like to access your grids from code behind in a different way (like suggested in your edit) you can always do the following: 编辑-选项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.
        }
}

If your goal is to display previews of the pages of the book inside a PivotItem in a grid-like manner [picture below], then you're better off placing GridView in a DataTemplate of Pivot.ItemTemplate and using data binding to display those pages automatically, this would eliminate the need to write the code in xaml.cs that you showed. 如果您的目标是以类似网格的方式在PivotItem中显示本书页面的预览(下图),那么最好将GridView放在Pivot.ItemTemplateDataTemplate ,并使用数据绑定显示这些页面自动,这样就无需在显示的xaml.cs中编写代码。

Please, share more details about your app (what you're given and what the end result should look like) so we could help you better. 请分享有关您应用程序的更多详细信息(给您的信息以及最终结果将是什么样的),以便我们更好地帮助您。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM