c# - 数据模板内的 UWP 网格

标签 c# xaml uwp pivot computer-science

我有一个 Pivot,我在 Pivot.HeaderTemplate 中设置标题,它基本上只是显示书籍的 Names。在我的 Pivot.ItemTemplate 中,我想显示一个 Grid,它是在我的 .xaml.cs 中构建的,但是因为 Grid 在我的 DataTemplate 我无法再在 .xaml.cs 的代码中访问 Grid x:Namebooks 是一个包含NameTitle

的书籍集合

主页.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>

现在我想在后面的代码中访问 BooksGrid 并实际创建 Grid

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 的错误。 如果我没有将 Grid 定义放在我的 DataTemplate 以及我的 Pivot 之外,我的 DrawGrid 就可以工作。因此,当 Grid 在我的 DataTemplate

中时,MainPage.xaml.cs 找不到它

我读到解决方案可能是我必须在 DataTemplate 加载后立即访问我想要使用的 Grid 实例。但我也不知道该怎么做。

第一个解决方案的编辑部分:

我还在另一种方法中使用 BooksGrid 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);


    }

最佳答案

您无法访问 BooksGrid 的原因是因为它将为 books 集合中的每本书动态生成。因此,对于每本书,都会生成一个 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>

在你后面的代码中:

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

关于c# - 数据模板内的 UWP 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48006719/

相关文章:

c# - WPF 将应用程序命令绑定(bind)到 ViewModel ICommand

c# - 我应该如何使用 MediaCapture 访问 UWP 应用程序中的摄像头?

c# - UWP Commandbar 到右侧

c# - OpenXML ASP.NET c# : tablecell vertical alignment issues

WPF XAML 设计器显示绑定(bind)的属性名称而不是属性值

c# - 如何更改 PdfPTable 中的字体大小?

wpf - DataGrid行详细信息可见性

c# - 如何保存/加载到具有不可序列化属性的磁盘类?

c# - 在运行时以静态方法获取当前类?

c# - 推送 bin/js 文件后,Servicestack 丢失 session ,直到缓存清除