MVVMCross中的RadioGroup实现

时间:2016-07-05 16:56:44

标签: c# xamarin mvvmcross

我有以下ViewModelradiogroup源代码。我在Male工作,我有两个radiobuttons(男性和女性)。最初,我想要选择<mvvmcross.droid.support.v7.appcompat.widget.MvxAppCompatRadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" local:MvxItemTemplate="@layout/item_radio" local:MvxBind="ItemsSource Items;ItemSelected SelectedItem" /> 单选按钮(默认)。但是,我当前的实现并未显示任何单选按钮被选中。它们都显示为未选中。我正在使用MvvmCross版本4.0

XML

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="12sp"
  local:MvxBind="Text Caption" />

Item_radio.xml

private List<Thing> _items = new List<Thing>()
{
   new Thing("Male"),
   new Thing("Female"),
};

public List<Thing> Items
{
  get { return _items; }
  set { _items = value; RaisePropertyChanged(() => Items); }
}

private Thing _selectedItem = new Thing("Male");
public Thing SelectedItem
{
  get 
  {
    return _selectedItem; 
  }
  set
  {
     _selectedItem = value;
     RaisePropertyChanged(() => SelectedItem);
  }
}

ViewModel.cs

 registry.RegisterFactory(new MvxCustomBindingFactory<MvxAppCompatRadioGroup>("ItemSelected", view => new MyRadioItemSelectedBinding(view)));

Setup.cs


[{
    "day": "06/19/2016",
    "region": "Ohio",
    "daily_er": "98.61"
}, {
    "day": "06/19/2016",
    "region": "Western NE",
    "daily_ef": "98.63"
}, {.........

1 个答案:

答案 0 :(得分:2)

问题似乎是你绑定到ItemSelected而不是SelectedItem

您的xml布局控件应该是:

<mvvmcross.droid.support.v7.appcompat.widget.MvxAppCompatRadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    local:MvxItemTemplate="@layout/item_radio"
    local:MvxBind="ItemsSource Items; SelectedItem SelectedItem" />