How to make ItemWrapGrid in UWP using C #?

Question How can I make a Grid with ItemsWrapGrid layout in C #?

Context

  • UWP
  • Visual studio 2015
  • Windows 10
  • FROM#

Background

I know how to do this in XAML. After creating a new UWP application in Visual Studio 2015 XAML:

 <Page x:Class="WrapGridTest001.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WrapGridTest001" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" MinWidth="10" MinHeight="10" > <Grid x:Name="thisGrid"> <GridView x:Name="thisGridView" IsItemClickEnabled="True"> <GridView.ItemsPanel> <ItemsPanelTemplate> <ItemsWrapGrid x:Name="thisItemsWrapGrid" Orientation="Horizontal" MaximumRowsOrColumns="5" /> </ItemsPanelTemplate> </GridView.ItemsPanel> <TextBlock Text="#1"/> <TextBlock Text="#2"/> <TextBlock Text="#3"/> </GridView> </Grid> </Page> 

To do this programmatically, I took it from XAML:

 <Page x:Class="WrapGridTest001.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WrapGridTest001" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" MinWidth="10" MinHeight="10" > </Page> 

and tried to play it in the Page constructor:

 public MainPage() { this.InitializeComponent(); // Make the Grid and set it to be the Page .Content: Grid grid = new Grid(); this.Content = grid; // Make the GridView for the Grid, then set it: GridView gridView = new GridView(); grid.Children.Add(gridView); // Make the ItemsPanelTemplate; necessary? ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate(); gridView.ItemsPanel = itemsPanelTemplate; // Make the ItemsWrapGrid. Can't figure out how to set this. ItemsWrapGrid itemsWrapGrid = new ItemsWrapGrid(); // When I traced the XAML version, its ItemsWrapGrid was apparently under // gridView.ItemsPanelRoot // , but that a get-only property. I can't find a place to set it. // Add the TextBlock with "#1", "#2", and "#3": for (int i = 1; i <= 3; ++i) { TextBlock textBlock = new TextBlock(); textBlock.Text = "#" + i.ToString(); gridView.Items.Add(textBlock); } // All done. However, this Grid will show contents with a vertical alignment // rather than a horizontal one. } 

What additional code do I need to add to install ItemsWrapGrid in C #, how was it installed in XAML? C # code is currently generating ItemsWrapGrid , but ItemsWrapGrid is not actually set up anywhere, since every attempt to use it so far has led to some error.

+4
source share
2 answers

what you need - XamlReader

 string template = "<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><ItemsWrapGrid VerticalAlignment = \"Top\" " + " ItemWidth = \"" + itemWidth + "\" Orientation = \"Horizontal\"/></ItemsPanelTemplate> "; yourgridview.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(template); 
+1
source

The fact is that ItemsWrapGrid is the default panel for the GridView. It is not good that ItemsPanelTemplate cannot have ItemsWrapGrid through a property and use XamlReader. After loading the gridview, it gets the default ItemsWrapGrid in the ItemsPanelRoot.

 yourgridview.Loaded += yourgridview_Loaded; 

and function

 private void yourgridview_Loaded(object sender, RoutedEventArgs e) { var a = sender as GridView; var b = a.ItemsPanelRoot; var c = b as ItemsWrapGrid; c.MaximumRowsOrColumns = 2;c.Orientation = Orientation.Horizontal; } 
0
source

All Articles