maui icon indicating copy to clipboard operation
maui copied to clipboard

More problems with the Picker

Open antonycartwright opened this issue 9 months ago • 0 comments

Description

Hi everyone,

There are more problems with the Picker control. Really basic problems. About a week ago, I explained that in a form, you can't initialise the value of the Picker on load. I explained a workaround also. In a simple form displaying one record, you can set the SelectedIndex from the codebehind, then subsequently, this calls whatever events are required for it to display the text.

However, when you're displaying many records, this gets very difficult. How can you do this from the codebehind when it's bound automatically? With massive difficulty.

Let's say we have a datatamplate, and within it we have this picker:

**<Picker Grid.Column="2" BindingContext="{x:Reference MyPage}" ItemsSource="{Binding Decks}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding Deck, Mode=OneTime}" SelectedIndexChanged="Picker_SelectedIndexChanged_1" />**

It binds fine, but with this bug, the initial value, or the valuje from the binding won't show. With my workaround, I could set the SelectedIndex, but how when you're showing many records? It needs to be able to do it itself while binding.

image

The documentation says this:

image

Here's another (ridiculous) workaround:

<Label x:Name="lblDeck" Grid.Column="2" Text="{Binding Deck.Name}" VerticalTextAlignment="Center" Padding="10,10,10,10"/> <Picker Grid.Column="2" BindingContext="{x:Reference MyPage}" ItemsSource="{Binding Decks}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding WordList.Deck, Mode=OneTime}" SelectedIndexChanged="Picker_SelectedIndexChanged_1" />

Basically, use a label as well as a Picker. Draw the label over the Picker and bind the label. The label will then show the correct value. Since the Picker doesn't initialise by default, it will be blank, and the label will show the value. Add a SelectedIndexChanged event to the Picker. When the value is changed, make the label invisible, as now the Picker can display! It's absolutely ridiculous, but ti works.

antonycartwright avatar Apr 29 '24 13:04 antonycartwright