【问题标题】:Xamarin Forms Picker Value BindingXamarin 表单选择器值绑定
【发布时间】:2021-04-18 17:33:26
【问题描述】:

绑定到选择器时,您可以使用ItemDisplayBinding 绑定显示的值,但我没有看到将每个项目映射到选择值的方法。因此,我不得不编写一些非常复杂的代码来使我的选择器与数据源更改保持同步。

原始模型

// NOTE: this implements INPC, just abbreviated for clarity
public class DataModel
{
  public ICollection<DataItem> Items;
  pubilc DataItem SelectedItem;
}

原始选择器:

<Picker Title="Select Item..." 
        ItemsSource="{Binding Items}"
        ItemDisplayBinding="{Binding Name}"
        SelectedItem="{Binding Path=SelectedItem}"></Picker>

新模型

// NOTE: this implements INPC, just abbreviated for clarity
public class DataModel
{
  public ICollection<DataItems> Items;
  public ICollection<string> ItemNames;
  public DataItem SelectedItem;
  public string SelectedItemName;
  public DataModel()
  {
     this.PropertyChanged += (s, e) => 
     {
       // I feel like I shouldn't have to do this...
       if(StringComparer.Ordinal.Equals(e.PropertyName, nameof(Items)))
       {
          if(!String.IsNullOrWhitespace(this.SelectedItemName))
          {
             this.SelectedItem = this.Items.FirstOrDefault(x => StringComparer.Ordinal.Equals(x.Name, this.SelectedItemName));
             if (this.SelectedItem == null) { this.SelectedItemName = null; }
          }
       }
  }
}

新选择器:

<Picker Title="Select Item..."
        ItemsSource="{Binding ItemNames}"
        SelectedItem="{Binding Path=SelectedItemName}"></Picker>

我希望能够做这样的事情:

<Picker Title="Select Item..."
        ItemsSource="{Binding Items}"
        ItemDisplayBinding="{Binding Name}"
        ItemValueBinding="{Binding Name}"
        SelectedItem="{Binding Path=SelectedItemName}"></Picker>

我不需要对项目的引用,我需要它的属性。这样,当 Items 集合更改时,如果它仍然存在,它会自动重新选择正确的项目。我发现我正在到处添加第二个集合,其中只有我想要选择的属性并进行所有这些映射。我工作过的所有其他平台,这很简单,所以我觉得我必须在 Xamarin.Forms 上遗漏一些东西。

【问题讨论】:

  • 我认为您不需要这样做。SelectedItem 属性数据绑定到连接视图模型的SelectedItem(在您的原始模型中)属性,其类型为@987654329 @。因此,当用户在 Picker 中选择一个项目时,SelectedItem 属性将自动设置为选中的DataItem 对象。

标签: xamarin.forms data-binding picker


【解决方案1】:

我认为您不需要这样做。SelectedItem 属性数据绑定到已连接视图模型的 SelectedItem(在您的原始模型中)属性,该属性是 DataItem 类型。因此,当用户在 Picker 中选择一个项目时,SelectedItem 属性会自动设置为选中的 DataItem 对象。

您可以在其 SelectedIndexChanged 事件中对其进行测试,例如:

<Picker Title="Select Item..." 
    ItemsSource="{Binding Items}"
    ItemDisplayBinding="{Binding Name}"
    SelectedItem="{Binding Path=SelectedItem}"
    SelectedIndexChanged="Picker_SelectedIndexChanged">
 </Picker>

 private void Picker_SelectedIndexChanged(object sender, EventArgs e)
    {
        Picker picker = sender as Picker;
        DataItem dt = picker.SelectedItem as DataItem ;
        Console.WriteLine(dt.Name); // you will see when you select a item,the SelectedItem will be changed automatically
    }

我建议你使用ObservableCollection&lt;Item&gt; Items,这样当它发生变化时它会自动更新你的Items

【讨论】:

  • 我知道 SelectedItem 将绑定在那里,我的 DataItem 对象将存储在那里。但是,后备列表对象发生了变化,因此它们不再是引用相等的,但它们将具有相同的 name 属性或 id 属性。这样就不会因为引用不同而重新选择该项目。
  • @MPavlak 是的,这取决于您如何更改数据,如果您替换现有项目,即使名称相同,因为它们的引用不同。
  • 正确,这就是正在发生的事情,也是我想要避免的事情。我绑定到一个名称属性,如果它允许值项目就像显示的名称一样,它将正确地重新选择。大多数其他框架都允许这样做,所以我很惊讶 Xamarin.Forms 对此一无所知。
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多