Cannot set combobox selected item opening a view

92 views Asked by At

I created a new project using template10 and I'm working to a simple form: I load a list of items from a remote server in a comboxbox and select one of the items after the list is loaded.
I tried setting SelectedValue, SelectedItem ot SelectedIndex, but when the Form is shown the listbox appears unselected.
Am I missing something?

this is the xaml

        <ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0" 
            RelativePanel.AlignLeftWith="parameterResizer"
            RelativePanel.Below="voceSpesaTextBlock"
            DisplayMemberPath="Descrizione"
            SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
            ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}}"
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

this is the code

        Voci = await vociSpesaTable.OrderBy(vs => vs.Descrizione).ToListAsync();
        VoceCorrente = Voci.FirstOrDefault(vs => vs.VoceSpesaNo == Item.VoceSpesaNo);

in the setter of the properties there is the call to the RaisePropertyChanged

1

There are 1 answers

1
Luca Morelli On

Looks loike I found the problem. For reasons I don't understand at full I have to set Mode=TwoWay for the ItemsSource property too.

            <ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0" 
                RelativePanel.AlignLeftWith="parameterResizer"
                RelativePanel.Below="voceSpesaTextBlock"
                DisplayMemberPath="Descrizione"
                SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
                ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}, Mode=TwoWay}"
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      />

This way it works