我已经将一个ListBox的ItemsSource设置如下:
<ListBox ItemsSource="{Binding abc}" />
我希望你能做什么
<ListBox>
<listBox.ItemsSource>
?????????????
<listBox.ItemsSource>
</ListBox>
<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>
<ListBox>
<listBox.ItemsSource>
<Binding Path = "abs" />
<listBox.ItemsSource>
</ListBox>
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>
来源 -
此处以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>
@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>
<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>
这段代码编译并执行正常。
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>
?????
中放什么?我不明白。 - Federico BerasateguiItemsControl
的ItemsSource
属性,因为智能感知工具提示的内容。实际上,ListBox
的ItemsSource
属性是从其基类继承的:ListBox
派生自ItemsControl
,而后者是定义该属性的位置。这就是为什么你的智能感知会显示它所显示的内容,事实上,在所展示的两种语法中,在设置哪个属性方面并没有区别。 - Dan Puzey