Xamarin Forms中Xaml中的Picker项源绑定

5

我需要在我的Xamarin Forms中创建状态选择器。我正在使用Xaml文件创建视图。

有人可以帮我将Picker与Xaml中的项源绑定吗?


1
http://stackoverflow.com/help/how-to-ask - Marvin Emil Brach
我上次检查时,发现在XAML中Picker没有ItemSource绑定属性。也就是说,您应该通过代码或自定义的BindablePicker进行绑定(例如:https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker)。 - Wizche
thecoshman - 有时候我们不知道从哪里开始,会来这里寻求建议/示例。 - jlo-gmail
4个回答

2

1
你无法在XAML中完成这个操作,可以在这里看到。
你需要在代码后台加载数据,使用常规API或者类似这样的东西。
你也可以将列表序列化为JSON或首选格式,并反序列化并将其传递给Picker

0

JordanMazurke提到,XLabs有这个功能。以下是一个示例:

<ContentPage x:Class="XLabs.Samples.Pages.Controls.ExtendedPickerPage"
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
         Title="Picker">
<ContentPage.Content>
    <StackLayout x:Name="myStackLayout">
        <Label Text="Xaml:" />
        <controls:ExtendedPicker x:Name="myPicker"
                                 DisplayProperty="FirstName"
                                 ItemsSource="{Binding MyDataList}"
                                 SelectedItem="{Binding TheChosenOne}" />
    </StackLayout>
</ContentPage.Content>


0
这个功能以前不存在,但最近通过新的ItemsSourceSelectedItem属性添加到了常规的Xamarin.Forms Picker中。它目前在版本2.3.4-pre1的预发布NuGet包中,但一旦发布,应该会出现在稳定的2.3.4+版本中。

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