2014-04-07 55 views
2

我正在構建一個表單,其中包含從列表中添加項目,但顯示給用戶的列表取決於從另一個列表中進行的選擇。爲此,我有兩個MvxSpinners,每個都有一個綁定的ItemsSource和SelectedItem。MvxSpinner selecteditem未更新itemssource更改

<MvxSpinner 
    android:layout_width="300dp" 
    android:layout_height="50dp" 
    local:MvxBind="ItemsSource item_source_one; SelectedItem selected_item_one;"/> 
<MvxSpinner 
    android:layout_width="300dp" 
    android:layout_height="50dp" 
    local:MvxBind="ItemsSource item_source_two; SelectedItem selected_item_two;"/> 

selected_item_one改變item_source_two變化到一個新的列表。所有這些工作,除了item_source_two更改selected_item_two保持與更改前相同。我希望selected_item_twoitem_source_two更改時視覺上顯示的內容相匹配。

回答

1

Spinner綁定在MvvmCross中並不完美 - 特別是SelectedItem更改必須在ItemsSource更改後發送。

要解決此問題,您可以在之後發送虛假SelectedItem更改,例如ItemsSource - 例如,是這樣的:

RaisePropertyChanged(() => item_source_two); 
RaisePropertyChanged(() => selected_item_two); 

如果你想更充分地解決這個問題,你也可以考慮重寫MvxSpinner - 遺憾的是,你不能輕易繼承,因爲我們錯過了虛擬的,但你這樣做:

public class MySpinner : MvxSpinner 
{ 
    public MySpinner(Context context, IAttributeSet attrs) 
     : base(context, attrs) 
    { 
    } 

    [MvxSetToNullAfterBinding] 
    public new IEnumerable ItemsSource 
    { 
     get { return base.ItemsSource; } 
     set 
     { 
      // TODO - work out what the current selection and store it in a local variable 
      base.ItemsSource = value; 
      // TODO - reset the current selection here from the local variable 
     } 
    } 
} 

https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxSpinner.cs

0

我不得不做這樣的繼承...

public class MySpinner : MvxSpinner 
{ 
    public MySpinner(Context context, IAttributeSet attrs):base(context,attrs) 
{ 
} 

[MvxSetToNullAfterBinding] 
public new IEnumerable ItemsSource 
{ 
    get { return base.ItemsSource; } 
    set 
    { 
     base.ItemsSource = null;    
     base.ItemsSource = value;  
    } 
} 
} 

之後,集合在您輸入時更新! :)