简体   繁体   English

如何在 Xamarin.Forms 的选取器中的 ItemDisplayBinding 中显示默认枚举值

[英]How can I display the default enum value in ItemDisplayBinding in the Picker of Xamarin.Forms

View:看法:

<Picker ItemsSource="{Binding ECCLevels}" 
        ItemDisplayBinding="{Binding QRCodeGenerator.ECCLevel}"
        HeightRequest="44"/>

ViewModel:视图模型:

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());

I simplified the code above.我简化了上面的代码。 The ObservableCollection gets filled with data after the second line of code. ObservableCollection 在第二行代码之后填充数据。 But the problem is I don't know what to put in the ItemDisplayBinding property of the picker since there the Enum gets converted into a list directly without going through a model.但问题是我不知道在选择器的 ItemDisplayBinding 属性中放什么,因为在那里 Enum 直接转换为列表而无需通过模型。

You can just use ItemDisplayBinding="{Binding .}" , the dot notation means that you refer to this basically.你可以只使用ItemDisplayBinding="{Binding .}" ,点符号意味着你基本上是指this So, this way you simply reference the object itself and not any other property.因此,通过这种方式,您只需引用对象本身而不是任何其他属性。

In the comments you asked me how I know, that is actually I great question.在评论中,你问我是怎么知道的,这实际上是我的好问题。 I think I picked it up as early as WPF.我想我早在 WPF 时就拿起了它。 But it's hard to dig up from the documentation, I found a small notice of it here: https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.binding.path?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Data_Binding_Path但是很难从文档中挖掘出来,我在这里发现了一个小通知: 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

Optionally, a period (.) path can be used to bind to the current source.可选地,可以使用句点 (.) 路径绑定到当前源。 For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}" .例如, Text="{Binding}"等效于Text="{Binding Path=.}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM