[英]Xamarin.Forms Picker ItemDisplayBinding not working with static list
[英]How can I display the default enum value in ItemDisplayBinding in the Picker of Xamarin.Forms
看法:
<Picker ItemsSource="{Binding ECCLevels}"
ItemDisplayBinding="{Binding QRCodeGenerator.ECCLevel}"
HeightRequest="44"/>
視圖模型:
public ObservableCollection<QRCodeGenerator.ECCLevel> ECCLevels { get; set; } = new ObservableCollection<QRCodeGenerator.ECCLevel>();
ECCLevels = new ObservableCollection<QRCodeGenerator.ECCLevel>(Enum.GetValues(typeof(QRCodeGenerator.ECCLevel)).OfType<QRCodeGenerator.ECCLevel>().ToList());
我簡化了上面的代碼。 ObservableCollection 在第二行代碼之后填充數據。 但問題是我不知道在選擇器的 ItemDisplayBinding 屬性中放什么,因為在那里 Enum 直接轉換為列表而無需通過模型。
你可以只使用ItemDisplayBinding="{Binding .}"
,點符號意味着你基本上是指this
。 因此,通過這種方式,您只需引用對象本身而不是任何其他屬性。
在評論中,你問我是怎么知道的,這實際上是我的好問題。 我想我早在 WPF 時就拿起了它。 但是很難從文檔中挖掘出來,我在這里發現了一個小通知: https : //docs.microsoft.com/en-us/dotnet/api/system.windows.data.binding.path?redirectedfrom=MSDN&view =network-4.7.2#System_Windows_Data_Binding_Path
可選地,可以使用句點 (.) 路徑綁定到當前源。 例如,
Text="{Binding}"
等效於Text="{Binding Path=.}"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.