当 SelectedItem 绑定到模型源并且 ItemsSource 绑定到模型选项时,SelectedIndex 不起作用

问题描述

对此的任何提示将不胜感激。我已经在我的视图模型中设置了我的选择器选项并将这个选定的选项保存回我的数据库,我已经将 SelectedItem 绑定到对象模型中的字段属性。但是,在设置认选定索引时,它不起作用。请帮忙!

代码

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

代码隐藏

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

型号

public void setupSelectedItem()
    {
        this.CurrentSelectedindex = 0;
    }

    public int CurrentSelectedindex { get; private set; } = 0;

`

解决方法

SelectedIndex 类型为 int,所选项目的索引,其中 默认为 -1。

SelectedItem 类型对象,选中的项目,其中 默认为空。

您不应同时绑定 Keras 2.4.3 tensorflow 2.3 Python 3.7 SelectedItem。他们做了同样的工作,选择一个就可以了。

SelectedIndex

如果还是不行, 我想知道 <Picker Grid.Column="2" Grid.Row="0" x:Name="myPicker" HeightRequest="40" WidthRequest="50" FontSize="14" Margin="0,15,110" ItemsSource="{Binding BindingContext.UserOptions,Source={x:Reference myPage }}" SelectedIndex="{Binding CurrentSelectedIndex}"/> 是您的 CurrentSelectedIndex 的财产还是 ViewModel 的财产?

更新,绑定到SelectedItem:

视图模型:

UserOptions

XML:

public class viewModel {

    List<string> UserOptions { get; set; }
    public string UnitAmount { get; set; }

    public viewModel()
    {
        //other codes

        setupSelectedItem();
    }

    public void setupSelectedItem()
    {
        this.UnitAmount = UserOptions[0];
    }
}