casillas
casillas

Reputation: 16793

Detect user selection from Spinner in mvvmcross

In the following code, I could able to display the CategoryList items in the spinner.

My question, how could I able to detect which one is chosen?

ViewModel.cs

public List<int> CategoryList
{
  get { return new List<int> { 1,2,3,4,5,6,7,8,9,10 }; }
}

int _chosenCategory; 
public int ChosenCategory { 
   get { return _chosenCategory; } 
   set { SetProperty(ref _chosenCategory, value); 
  } 
}

ViewModel.axml

<LinearLayout
  android:layout_weight="1"
  android:layout_width="0dp"
  android:layout_height="match_parent">
    <mvvmcross.droid.support.v7.appcompat.widget.MvxAppCompatSpinner
         android:layout_width="100dp"
         android:layout_height="match_parent"
         local:MvxItemTemplate="@layout/xxx"
         local:MvxBind="ItemsSource CategoryList;SelectedItem ChosenCategory" />
</LinearLayout>

Upvotes: 0

Views: 387

Answers (1)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

You can bind it via SelectedItem

<mvvmcross.droid.support.v7.appcompat.widget.MvxAppCompatSpinner
     android:layout_width="100dp"
     android:layout_height="match_parent"
     local:MvxItemTemplate="@layout/category_spinner"
     local:MvxBind="ItemsSource CategoryList; SelectedItem ChosenCategory" />

And ensure that you have this in you Setup class:

public class Setup : MvxAndroidSetup
{
    // ...

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        MvxAppCompatSetupHelper.FillTargetFactories(registry);
        base.FillTargetFactories(registry);
    }
}

Upvotes: 6

Related Questions