Xamarin 表单选择器 SelectedItem 绑定 [英] Xamarin Forms Picker SelectedItem Binding

查看:25
本文介绍了Xamarin 表单选择器 SelectedItem 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin 表单文档 Xamarin.Forms.Picker.SelectedItem 表示 Picker 有一个公共属性 SelectedItem.但是,当我尝试绑定到它时出现错误.如果您必须手动处理 SelectedIndex 属性,则选择器不是很有用.

The Xamarin Forms doc Xamarin.Forms.Picker.SelectedItem says there is a public property SelectedItem for Picker. However, I get an error when I try to bind to it. The picker is not very useful if you have to manually handle the SelectedIndex property.

托尼

推荐答案

无需手动处理 SelectedIndex.您可以使用 PickerSelectedItem 属性.只要确保您的类型相同即可.例如,如果您的 ItemsSource 绑定到一个属性:

No need to manually handle the SelectedIndex. You can use the Picker's SelectedItem property. Just make sure your types are the same. For example, if your ItemsSource is bound to a property:

BookTitles List<string> { get; set; }

您的 SelectedItem 必须类似于:

SelectedBookTitle string { get; set; }

确保设置 SelectedBookTitle 值以在首次显示页面时显示标题.不要忘记在 SelectedItem Binding 上将 Mode 设置为 TwoWay.例如:

Make sure to set the SelectedBookTitle value to show a title when the page is first shown. Do not forget to set Mode to TwoWay on the SelectedItem Binding. for example:

<Picker ItemsSource="{Binding BookTitles}" SelectedItem="{Binding 
SelectedBookTitle, Mode=TwoWay}" />

这将确保在第一次显示页面时显示标题,并保持 SelectedBookTitle 在页面和代码隐藏/视图模型上的值相等.

This will ensure the title is shown when the page is first displayed, and keeps the value of SelectedBookTitle equal on the Page and codebehind/viewmodel.

在这个例子中不需要使用行为.

No need to use behaviours in this example.

这篇关于Xamarin 表单选择器 SelectedItem 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆