如何使用C#在UWP中创建ItemsWrapGrid?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用C#在UWP中创建ItemsWrapGrid?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2918字,纯文字阅读大概需要5分钟。
内容图文
![如何使用C#在UWP中创建ItemsWrapGrid?](/upload/InfoBanner/zyjiaocheng/772/9d41a7e406c6446ca3850f808fb37946.jpg)
问题:如何在C#中使用ItemsWrapGrid布局制作网格?
上下文
> UWP
> Visual Studio 2015
> Windows 10
> C#
背景
我知道如何在XAML中制作它.在Visual Studio 2015中创建新的UWP应用程序后,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>
为了以编程方式进行编程,我已将其从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>
并尝试在Page的构造函数中重现它:
public MainPage()
{
this.InitializeComponent();
// Make the Grid and set it to be the Page's .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's a get-only property. I can't find a place to set it.
// Add the TextBlock's 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.
}
我需要添加哪些附加代码来在C#中设置ItemsWrapGrid,因为它是在XAML中设置的?目前C#代码创建了ItemsWrapGrid,但ItemsWrapGrid实际上并未在任何地方设置,因为到目前为止每次尝试使用它都会导致某种类型的错误.
解决方法:
你需要的是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);
内容总结
以上是互联网集市为您收集整理的如何使用C#在UWP中创建ItemsWrapGrid?全部内容,希望文章能够帮你解决如何使用C#在UWP中创建ItemsWrapGrid?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。