2017-10-03 15 views
1

DataGridViewを使用してWinFormsアプリケーションを作成しています。 DataSourceはReactiveListです。ただし、リストに新しい項目を追加してもUIは更新されません。ReactiveListの使い方アイテムが追加/削除/変更されたときにUIが更新される

のViewModel

public class HomeViewModel: ReactiveObject 
{ 
    public ReactiveCommand<object> AddCmd { get; private set; } 

    ReactiveList<Model> _models; 
    public ReactiveList<Model> Models 
    { 
     get { return _models; } 
     set { this.RaiseAndSetIfChanged(ref _models, value); } 
    } 

    public HomeViewModel() 
    { 
     Models = new ReactiveList<Model>() { new Model { Name = "John" } }; 

     AddCmd = ReactiveCommand.Create(); 
     AddCmd.ObserveOn(RxApp.MainThreadScheduler); 
     AddCmd.Subscribe(_ => 
     { 
      Models.Add(new Model { Name = "Martha" }); 
     }); 
    } 
} 

public class Model 
{ 
    public string Name { get; set; } 
} 

ビュー

public partial class HomeView : Form, IViewFor<HomeViewModel> 
{ 
    public HomeView() 
    { 
     InitializeComponent(); 
     VM = new HomeViewModel(); 

     this.OneWayBind(VM, x => x.Models, x => x.gvData.DataSource); 
     this.BindCommand(VM, x => x.AddCmd, x => x.cmdAdd); 
    } 

    public HomeViewModel VM { get; set; } 

    object IViewFor.ViewModel 
    { 
     get { return VM; } 
     set { VM = (HomeViewModel)value; } 
    } 

    HomeViewModel IViewFor<HomeViewModel>.ViewModel 
    { 
     get { return VM; } 
     set { VM = value; } 
    } 
} 
  1. ビューは常に "ジョン" を示しています。
  2. デバッグ登録した項目を追加してください。
  3. ObservableCollectionと同じ結果を試しました。 How to use ReactiveList so UI is updated when new items are added
  4. IReactiveDerivedListと同じ結果を試してみました。 Does ReactiveUI RaiseAndSetIfChanged fire for List<T> Add, Delete, Modify?
+0

あなたのコマンドは簡略化できます。 AddCmd = ReactiveCommand.Create(()=> Models.Add(.....)); –

答えて

0

ReactiveListではなくReactiveBindingListが必要だと思います。これはバインディングのためのReactiveListのWinForms固有のバージョンです。

+0

ありがとうございます。私がReactiveBindingListを使用する場合、私のViewModelsを共有できませんし、ReactiveBindingListはModels [0] .Name = "Palma"のような変更をしないでください。 – arturogranillo

+0

あなたは最初のVMからWinFormsVMを派生させてCreateDerviedBindingLists()あなたのメインVMのReactiveListの上に、ロジックをプラットフォーム間で共有することができます。 –

関連する問題