如何从 uwp 中的代码后面添加水平方向的 ListView?
how to add ListView with orientation as horizontal from code behind in uwp?
我必须从后面的代码中实现此语法,我该怎么做
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal"
/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
一般来说,我怎样才能从 uwp 中的代码隐藏中实现 属性 元素语法?
how to add ListView with orientation as horizontal from code behind in uwp?
为了在后面的代码中制作ItemsPanelTemplate
,您可以解析ItemsPanelTemplate
字符串,然后将其传递给ListView的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;
}
你可以这样实现。
<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);
}
}
我必须从后面的代码中实现此语法,我该怎么做
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal"
/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
一般来说,我怎样才能从 uwp 中的代码隐藏中实现 属性 元素语法?
how to add ListView with orientation as horizontal from code behind in uwp?
为了在后面的代码中制作ItemsPanelTemplate
,您可以解析ItemsPanelTemplate
字符串,然后将其传递给ListView的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;
}
你可以这样实现。
<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);
}
}