[英]Strange behavior of ListView with WrapGrid in Windows Phone 8.1 XAML
[英]WrapGrid in ListView (without xaml) with C#
有人知道如何將以下xaml代碼轉換為C#嗎?
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
我不確定為什么要在代碼中創建此代碼,最好將ItemPanelTemplate放在頁面或App.xaml中的資源中。
否則,這里是如何通過代碼創建它:
ItemsPanelTemplate template=new ItemsPanelTemplate();
var str = "<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+"<WrapGrid Orientation=\"Horizontal\" />"
+"</ItemsPanelTemplate>";
ItemsPanelTemplate panelTemplate = (ItemsPanelTemplate)Windows.UI.Xaml.Markup.XamlReader.Load(str);
ListView listView=new ListView();
listView.ItemsPanel = panelTemplate ;
通過此鏈接,我得出的結論是ItemsPanelTemplate
主要屬於XAML,因為您無法通過簡單的運行時API對其進行更改。 因此,您需要堅持使用XAML聲明,也可以使用XamlReader
加載XAML。
using Windows.UI.Xaml.Markup;
private ListView GetListView()
{
const string xaml = @"<ListView xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation=""Horizontal"" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>";
var lv = (ListView)XamlReader.Load(xaml);
return lv;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.