如何从UWP背后的代码中添加listView和方向为水平?

发布于 01-21 05:14 字数 366 浏览 4 评论 0原文

我必须从ADER背后的代码中实现此语法,该如何

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

从UWP背后的代码中实现属性元素语法?

I have to achieve this syntax from code behind how can I do that

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

in general how can I achieve property element syntax from code behind in uwp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释2025-01-28 05:14:31

您可以这样实现。

<ListView x:Name="AwesomeListView" ItemsSource="{x:Bind Items}">
    <!--
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    -->
</ListView>
public sealed partial class MainPage : Page
{
    public List<int> Items { get; set; } = new List<int>() { 1, 2, 3, 4, 5 };

    public MainPage()
    {
        this.InitializeComponent();

        this.AwesomeListView.ItemsPanel = CreateItemsPanelTemplate(Orientation.Horizontal);
    }

    ItemsPanelTemplate CreateItemsPanelTemplate(Orientation orientation)
    {
        string xamlString =
            $@"<ItemsPanelTemplate
                    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ItemsStackPanel Orientation='{orientation}' />
                </ItemsPanelTemplate>";

        return (ItemsPanelTemplate)XamlReader.Load(xamlString);
    }
}

You can achieve it this way.

<ListView x:Name="AwesomeListView" ItemsSource="{x:Bind Items}">
    <!--
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    -->
</ListView>
public sealed partial class MainPage : Page
{
    public List<int> Items { get; set; } = new List<int>() { 1, 2, 3, 4, 5 };

    public MainPage()
    {
        this.InitializeComponent();

        this.AwesomeListView.ItemsPanel = CreateItemsPanelTemplate(Orientation.Horizontal);
    }

    ItemsPanelTemplate CreateItemsPanelTemplate(Orientation orientation)
    {
        string xamlString =
            $@"<ItemsPanelTemplate
                    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ItemsStackPanel Orientation='{orientation}' />
                </ItemsPanelTemplate>";

        return (ItemsPanelTemplate)XamlReader.Load(xamlString);
    }
}
深空失忆2025-01-28 05:14:31

如何从UWP背后的代码中添加ListView?

的水平。

对于在后面的代码中制作itemSpaneltemplate,您可以分析itemspaneltemplate字符串,然后将其传递到ListView的iteg> itemspanel

private void ApplyItemsStackPanelAsItemsPanel(ItemsControl itemsControl)
{

    var itemsPanelTemplateXaml =
        $@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                          xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
           <ItemsStackPanel Orientation='Horizontal'/>
       </ItemsPanelTemplate>";

    itemsControl.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(itemsPanelTemplateXaml);

}

用法

private void BuildListView()
{
    var listview = new ListView();
    ApplyItemsStackPanelAsItemsPanel(listview);
    listview.ItemsSource = new string[] { "1", "2", "3", "4" };
    this.Content = listview;
}

how to add ListView with orientation as horizontal from code behind in uwp?

For making ItemsPanelTemplate in the code behind, you could parse ItemsPanelTemplate string and then pass it to ListView's ItemsPanel.

private void ApplyItemsStackPanelAsItemsPanel(ItemsControl itemsControl)
{

    var itemsPanelTemplateXaml =
        $@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                          xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
           <ItemsStackPanel Orientation='Horizontal'/>
       </ItemsPanelTemplate>";

    itemsControl.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(itemsPanelTemplateXaml);

}

Usage

private void BuildListView()
{
    var listview = new ListView();
    ApplyItemsStackPanelAsItemsPanel(listview);
    listview.ItemsSource = new string[] { "1", "2", "3", "4" };
    this.Content = listview;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文