简体   繁体   中英

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. 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.

You can just use ItemDisplayBinding="{Binding .}" , the dot notation means that you refer to this basically. 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. 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

Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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