如何在XAML中设置ItemsSource?

6

我已经将一个ListBox的ItemsSource设置如下:

<ListBox ItemsSource="{Binding abc}" />

我希望你能做什么


<ListBox>
    <listBox.ItemsSource>
        ?????????????
    <listBox.ItemsSource>
</ListBox>

你期望在 ????? 中放什么?我不明白。 - Federico Berasategui
为什么要这样做?这没有意义。在第一个示例中,您已经使用了绑定语法。为什么要将其转换为更冗长的语法? - Federico Berasategui
因为我的 ListBox 是 ItemsControl 的子级。而且当我将鼠标悬停在 ItemsSource 上时,它会显示 ItemsControl.ItemsSource,所以我想在我的 ListBox 上明确声明 ItemsSource。 - Vishal
3
@Vishal:我认为你错误地认为你的第一个代码片段是在设置父级ItemsControlItemsSource属性,因为智能感知工具提示的内容。实际上,ListBoxItemsSource属性是从其基类继承的:ListBox派生自ItemsControl,而后者是定义该属性的位置。这就是为什么你的智能感知会显示它所显示的内容,事实上,在所展示的两种语法中,在设置哪个属性方面并没有区别。 - Dan Puzey
1
@DanPuzey,将我的代码更改为已接受答案的代码后,我真的知道两种语法之间没有任何区别。 - Vishal
显示剩余2条评论
5个回答

18
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">

<ListBox>
<ListBox.ItemsSource>
    <x:Array Type="sys:String">
        <sys:String>1st item</sys:String>
        <sys:String>2nd item</sys:String>
    </x:Array>
<ListBox.ItemsSource>
</ListBox>

</Window>

5
<ListBox>
    <listBox.ItemsSource>
        <Binding Path = "abs" />
    <listBox.ItemsSource>
</ListBox>

3
这正是原帖第一个例子中的情况,只不过使用了更冗长的语法表达。 - Federico Berasategui
1
你认为@HighCore的问题是什么? - Arsen Mkrtchyan
这正是我想要的。谢谢你,由于stackoverfolw.com的一些限制,我将在9分钟后接受你的答案。 - Vishal
1
@HighCore:我认为这正是OP所询问的,但我认为他们提出问题的原因是错误的(请参见我的评论)。 - Dan Puzey

3

Xamarin示例

如果您在寻找Xamarin示例(问题似乎与XAML有关),那么您可以尝试以下内容 -

<Picker x:Name="picker"
    Title="Select a monkey"
    TitleColor="Red">
  <Picker.ItemsSource>
    <x:Array Type="{x:Type x:String}">
       <x:String>Baboon</x:String>
       <x:String>Capuchin Monkey</x:String>
       <x:String>Blue Monkey</x:String>
       <x:String>Squirrel Monkey</x:String>
       <x:String>Golden Lion Tamarin</x:String>
       <x:String>Howler Monkey</x:String>
       <x:String>Japanese Macaque</x:String>
     </x:Array>
  </Picker.ItemsSource>
</Picker>

来源 -

https://learn.microsoft.com/zh-cn/xamarin/xamarin-forms/user-interface/picker/populating-itemssource#populating-a-picker-with-data

此处以Picker为例,但是ItemsSource语法可根据外部控件互换使用,例如:

<ListView>
  <ListView.ItemsSource>
      <x:Array Type="{x:Type x:String}">
        <x:String>mono</x:String>
        <x:String>monodroid</x:String>
        <x:String>monotouch</x:String>
        <x:String>monorail</x:String>
        <x:String>monodevelop</x:String>
        <x:String>monotone</x:String>
        <x:String>monopoly</x:String>
        <x:String>monomodal</x:String>
        <x:String>mononucleosis</x:String>
      </x:Array>
  </ListView.ItemsSource>
</ListView>

1

@HighCore、@DanPazey 和 @Vishal:

实际上,标记绑定语法可能会证明其是有用的,甚至是必需的。

更不用说多重绑定了,考虑以下情况。

假设你需要将 ListBox 绑定到 CollectionViewSource(用于排序或其他)。像这样:

<Window.Resources>
    <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
</Window.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource abc_CVS_Key}}">
    </ListBox>

您可能由于技术原因希望将CVS资源的范围限制在特定的ListBox中。
如果您在属性中写下ItemsSource绑定。
    <ListBox ItemsSource="{Binding Source={StaticResource abc_CVS_Key}}">
        <ListBox.Resources>
            <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
        </List.Resources>
    </ListBox>

你的代码可以编译,但在运行时程序将无法找到abc_CVS_Key资源键,因为该资源在代码中稍后定义。你需要在ListBox的ItemsSource绑定中引用它之前先定义该资源。像这样:

    <ListBox>
        <ListBox.Resources>
            <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
        </List.Resources>
        <ListBox.ItemsSource>
            <Binding Source="{StaticResource abc_CVS_Key}" />
        </ListBox.ItemsSource>
    </ListBox>

这段代码编译并执行正常。


0
对于WinUI 3:
在WinUI 3中,我们没有x:Array。但是我们可以创建类似的东西:
namespace SampleApp.Helpers;

public class XamlList : List<object>
{
}

使用方法如下:
<ItemsRepeater>
    <ItemsRepeater.ItemsSource>
        <helpers:XamlList>
            <x:String>Item A</x:String>
            <x:String>Item B</x:String>
            <x:String>Item C</x:String>
        </helpers:XamlList>
    </ItemsRepeater.ItemsSource>
</ItemsRepeater>

如果你想看它的实际效果,这里有一个视频 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接