【问题标题】:SelectedIndex not working when SelectedItem is bound to model source and ItemsSource bound to model options当 SelectedItem 绑定到模型源并且 ItemsSource 绑定到模型选项时,SelectedIndex 不起作用
【发布时间】:2021-04-17 09:11:16
【问题描述】:

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

代码

<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}"/>

代码隐藏

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;

`

【问题讨论】:

  • 你的模特在哪里?为什么您为 ItemsSource 指定了来源,但没有为其他绑定属性指定来源?
  • 嗨,杰森,感谢您的帮助。我有一个 myPage 的视图模型以及一个我保存回 mongoDB 的数据的模型......这很棘手,因为这个选择器有助于列表视图。第一篇:stackoverflow.com/questions/65672124/…
  • 您的帖子需要包含足够的信息来充分说明问题。引用其他帖子中包含的代码是不够的
  • 好的,您还需要什么信息?
  • "你的模型在哪里?" - 即,发布您的模型的代码

标签: c# xamarin binding picker


【解决方案1】:

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

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

您不应同时绑定SelectedItemSelectedIndex。他们做了同样的工作,选择一个就可以了。

<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 }}"                                                                               
    SelectedIndex="{Binding CurrentSelectedIndex}"/>

如果还是不行, 我想知道CurrentSelectedIndex 是您的ViewModel 的属性还是UserOptions 的属性?

更新,绑定到 SelectedItem:

视图模型:

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];
    }
}

Xaml:

<StackLayout>

    <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 }"
        />
</StackLayout>

【讨论】:

  • 嗨,杰克,感谢您的帮助。这个解决方案的唯一问题是,如果我删除了对 UnitAmount 的 SelectedItem 引用,那么它不会保存回我的模型以保存到我的 mongoDB 中。 CurrentSelectedIndex 是我的视图模型的一个属性,我很高兴将其硬编码为 0,因此在加载时选择了第一项...
  • 那么你可以选择去掉“{Binding CurrentSelectedIndex}”,改为绑定SelectedItem="{Binding UnitAmount }"。然后首先将 UnitAmount 硬编码为 itemSourec 的第一个元素。它还将选择第一个元素。
  • 谢谢 Jack,你能举一个简单的例子来说明如何做到这一点吗?
  • @Avatar 我的解决方案对您有用吗?如果是,请您接受(点击此答案左上角的☑️),以便我们可以帮助更多有相同问题的人:)。
  • 嗨,杰克,我在列表中添加了一个范围,如下所示 listOptions.AddRange(unitAmounts); this.unitAmounts = listOptions[0];我也试过 this.unitAmounts[0] = listOptions[0];但这也不起作用。我不确定是否有可能由于数据绑定加载到列表和单独的数据绑定将列表保存到我的 mongodd 模型的冲突。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2015-01-04
  • 2018-08-02
  • 2021-08-15
  • 2012-05-31
  • 2013-09-26
  • 2012-09-20
相关资源
最近更新 更多