2021年1月17日星期日

SelectedIndex not working when SelectedItem is bound to model source and ItemsSource bound to model options

Any tips on this would be appreciated. I have set my picker's options in my view model and to save this selected option back to my DB, I have bound the SelectedItem to the field attribute within the object's model. However then when setting the default selected index, it doesn't work. Please help!

code

<Picker Grid.Column="2"      Grid.Row="0"      x:Name="myPicker"      HeightRequest="40"      WidthRequest="50"      FontSize="14"      Margin="0,15,0,110"                           ItemsSource="{Binding BindingContext.UserOptions,  Source={x:Reference myPage }}"                                                                                     SelectedItem="{Binding .UnitAmount }"      SelectedIndex="{Binding CurrentSelectedIndex}"/>  

code-behind

private int currentSelectedIndex = 0;      public int CurrentSelectedIndex      {          get => currentSelectedIndex;          set          {              currentSelectedIndex = value;              OnPropertyChanged(nameof(CurrentSelectedIndex));          }      }  

Model

public void setupSelectedItem()      {          this.CurrentSelectedIndex = 0;      }        public int CurrentSelectedIndex { get; private set; } = 0;  

`

https://stackoverflow.com/questions/65683582/selectedindex-not-working-when-selecteditem-is-bound-to-model-source-and-itemsso January 12, 2021 at 08:03PM

没有评论:

发表评论