【问题标题】:UWP GridView in C# with DataTemplateC# 中的 UWP GridView 和 DataTemplate
【发布时间】:2017-12-31 00:54:18
【问题描述】:

我有一个Grid,我想在.xaml.cs 中的代码隐藏的每个单元格中放置一个GridView。问题是我不知道如何用DataTemplate 创建一个GridView 来在C# 中绑定东西

DataTemplate Template= new DataTemplate();
GridView gridView = new GridView();
gridView.ItemsSource = book;

这就是我所拥有的。感谢您的帮助

J.

【问题讨论】:

    标签: c# wpf gridview uwp


    【解决方案1】:

    我想从 .xaml.cs 的代码隐藏中将 GridView 放入其中的每个单元格。

    您可以在 App.xaml 文件中创建一个DataTemplate,如下所示。

    <Application.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="template">
                <StackPanel>
                    <TextBlock Text="{Binding }"/>
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>
    

    并在后面的代码中使用。

    private void SetUpUI()
    {
        var gridView = new GridView();
        //DataTemplate usage.
        gridView.ItemTemplate = (DataTemplate)App.Current.Resources["template"];
        gridView.ItemsSource = new List<string> { "fas", "fasd", "fasf" };
        RootLayout.Children.Add(gridView);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 2019-01-08
      • 1970-01-01
      • 2020-02-01
      • 2016-01-26
      • 1970-01-01
      • 2016-08-06
      • 2019-04-13
      • 1970-01-01
      相关资源
      最近更新 更多