2
votes

Is there a way to dynamicaly assign Picker.Items from XAML?

Something like:

<Picker x:Name="pckTheme">
    <Picker.Items>
        <x:String>{Binding Option1Text}></x:String>
        <x:String>{DynamicResource Option2Text}></x:String>
    </Picker.Items>
</Picker>

(This sample is not working, of course).

As far as I can tell, the only allowed type in Picker.Items is x:String and I can't seem to find any method to bind to x:String.

Doing it from code is relatively simple, but I would prefer doing it from XAML directly, if possible.

Goal here is to localize content from resource file, but other uses may come handy.

2
What about binding the values ? doesn't that work for you? - pinedax
That is what I am trying to do. It doesn't seem to be working for <x:String> . - bocko
I think you could read the information (your strings) from the resource files and inserted it into a list which is then bound to the Picker... - pinedax
Thanks. That's the way I am doing it now. But then I also need to handle runtime language change from code, and that's why I wanted to do it from XAML. - bocko

2 Answers

1
votes

Normally you would bind the Picker to a list of items which can be as dynamic as you want. To put them in there as hardcoded items isn't really dynamically assigning if you ask me. What you could do is create a ValueConverter to use in your XAML that gets the language specific value of a key and bind the Picker to a list of keys.

1
votes

Please use the following XAML code:

<x:Array Type="{x:Type x:String}">
<x:Static Member="local:AppResources.Events"/>
<x:Static Member="local:AppResources.Device"/>
<x:Static Member="local:AppResources.Biographical"/>
</x:Array>

Where local is:

xmlns:local="clr-namespace:YOURNAMESPACE"