1

基本的にマスター用の View-Model と詳細用の View-Model を作成するコード例をいくつか見てきました。次に、Master を DataGrid を使用して View に接続し、Master View-Model の選択された項目を (DetailViewModel の) ObservableObject タイプの SelectedItem プロパティにバインドし、それを Detail View-Model に送信します。その後、詳細ビューにバインドされます...などは、ここでお勧めします または、このようなもの

これを読んだ後... http://blogs.msdn.com/b/adonet/archive/2011/03/08/ef-feature-ctp5-code-first-model-with-master-detail-wpf-application. aspx

これは.. 1 つのモデル エンティティ、複数のページ -> 複数のビュー? 複数のViewModel?

そしてこれは...単一のビューに関連付けられた複数のViewModel

私は非常に混乱しています:)これを行うための推奨されるPrismの方法は、マスターと詳細に別々のViewModelを使用することのようですが、私の原因では、そのように行うにはかなりの作業が必要になると感じています。

Entity Framework DbContext を使用していて、バインドに .Local プロパティを使用している場合、Master/Detail の状況で 1 つの View-Model を使用する方が理にかなっているように思えます。

4

1 に答える 1

2

DbContext またはそのプロパティのいずれかに直接バインドすることは、MVVM の背後にある考え方に違反するビューにモデルを公開するため、悪い考えです。(View は ViewModel を認識し、ViewModel は Model を認識します)。

マスター/ディテール シナリオでは、それぞれが異なる役割を持っているため、それぞれ 2 つの異なるビューを持つ 2 つの異なるビューモデルがあります。

  • マスターの役割は、選択肢のリストとその詳細に移動する方法を提示することです。
  • 一方、詳細の役割は、おそらく値を変更する方法を使用して、単一の要素を提示することです。

データ モデルとして製品のリストがあり、各製品には ID、名前、および価格があるとします。

class Product {
    public int Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
}

また、製品のリストを含むある種のデータ モデルがあります。

class ProductRepository {
    private List<Product> products = new List<Product>();
    public List<Product> Products 
    { 
        get { return this.prodcuts; }
    }
}

次に、MasterViewModel の役割はProducts、ProductRepository モデルのリストを公開し、詳細ビューに切り替える方法を提供することです。

class ProductsViewModel {
    private ProductRepositry productsModel = new ProductRepository();
    private ObservableCollection<Product> products = new ObservableCollection<Product>(productsModel.Products);

    public ObservableCollection<Product> Products
    {
        get { return this.products; }
    }

    public ProductViewModel Detail { get... private set... } // setter includes PropertyChange

    public ICommand ViewDetail { get... }

    public void ViewDetail(Product detail)
    {
        this.Detail = new ProductViewModel(detail);
    }
}

ProductViewModel唯一の責任は、以下を提示することProductです。

class ProductViewModel {
    public string Name { get... set... } // Again a PropertyChange would be necessary for propert binding
    public int Price { get... set... } // dito
}
于 2012-12-17T14:53:41.717 に答える