MvxPickerViewModel绑定到对象的属性

eMi*_*eMi 3 uipickerview xamarin.ios 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();
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

Stu*_*art 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();
    }
Run Code Online (Sandbox Code Playgroud)

来自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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 或覆盖ToString你的Mandant对象

  • 或提供并使用一些ValueConverters从IEnumerable<Mandant>IEnumerable<WrappedMandant>MandantWrappedMandant这里WrappedMandant 提供ToString()您要使用的实现:

      public class WrappedMandant
      {
           public Mandant Mandant { get;set;}
           public override string ToString()
           {
               return Mandant.FirstName;
           }
      }
    
    Run Code Online (Sandbox Code Playgroud)

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