MvxPickerViewModel绑定到对象的属性

时间:2013-08-27 11:06:51

标签: xamarin.ios uipickerview mvvmcross

我有以下代码:

var pickerViewModel = new MvxPickerViewModel(this.mandantenPicker);
this.mandantenPicker.Model = pickerViewModel;
this.mandantenPicker.ShowSelectionIndicator = true;
mandantenAuswahlTextfield.InputView = this.mandantenPicker;

var set = this.CreateBindingSet<LoginView, LoginViewModel>();

set.Bind(pickerViewModel).For(p => p.SelectedItem).To(vm => vm.SelectedMandant);
set.Bind(pickerViewModel).For(p => p.ItemsSource).To(vm => vm.Mandanten);

set.Apply();

在PickerView中,我没有预期的值。它显示了对象的命名空间。

我绑定到“Mandants”列表,Mandant有FirstName / LastName。如何设置“显示”值?

类似于:

set.Bind(pickerViewModel).For(p => p.ItemsSource).To(vm => vm.Mandanten)["FirstName"]; //stupid example but to show better what I mean/want

1 个答案:

答案 0 :(得分:6)

MvxPickerViewModel目前使用GetTitle()来计算要显示的内容:

    public override string GetTitle(UIPickerView picker, int row, int component)
    {
        return _itemsSource == null ? "-" : RowTitle(row, _itemsSource.ElementAt(row));
    }

    protected virtual string RowTitle(int row, object item)
    {
        return item.ToString();
    }

来自https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxPickerViewModel.cs

如果您想自定义显示的字符串,您可以:

  • 继承自MvxPickerViewModel并为RowTitle提供覆盖:

    protected override string RowTitle(int row, object item)
    {
        return ((Mandant)item).FirstName;
    }
    
  • 或覆盖ToString对象中的Mandant

  • 或提供并使用从IEnumerable<Mandant>IEnumerable<WrappedMandant>MandantWrappedMandant的一些ValueConverters,其中WrappedMandant提供ToString()实施你想用:

      public class WrappedMandant
      {
           public Mandant Mandant { get;set;}
           public override string ToString()
           {
               return Mandant.FirstName;
           }
      }
    

如果您想要自定义进一步显示的选择器单元格,那么您也可以覆盖GetView,并为每个Mandant提供自定义单元格