简体   繁体   English

C#在ListView中的WrapGrid(无xaml)

[英]WrapGrid in ListView (without xaml) with C#

Somebody know how to translate the following xaml code to C#? 有人知道如何将以下xaml代码转换为C#吗?

<ListView>
     <ListView.ItemsPanel>
          <ItemsPanelTemplate>
                <WrapGrid Orientation="Horizontal" />
          </ItemsPanelTemplate>
     </ListView.ItemsPanel>
</ListView>

I'm not sure why you want to create this in code, it would probably be better to just put the ItemPanelTemplate in a resource in your page or in App.xaml. 我不确定为什么要在代码中创建此代码,最好将ItemPanelTemplate放在页面或App.xaml中的资源中。
Otherwise here how to create it by code: 否则,这里是如何通过代码创建它:

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 ;

From this link what I conclude is that ItemsPanelTemplate belongs to mainly XAML because you can not alter them from simple run time APIs. 通过链接,我得出的结论是ItemsPanelTemplate主要属于XAML,因为您无法通过简单的运行时API对其进行更改。 So you need to stick to either XAML declaration or you can use XamlReader to load the XAML. 因此,您需要坚持使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM