えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

MVVM パターンの ViewModel は Tree 構造で管理する

MVVM パターンの ViewModel の作成の仕方について聞かれることがたまにある。

MVVM パターンの ViewModel は View に対して1つ作る。
Window や UserControl に対して作成するのは分かりやすいが Panel やその継承など 関連性が高い情報をまとめて  ViewModel にすることもできる。
ItemsControl やその継承は ViewModel のコレクションとして持つことになる。

View は、VisualTreeHelper や Snoop を見ればわかるように Tree 構造になっている。
したがって、 ViewModel も Tree 構造になるべきだ。
具体的には、親は VisualTree に合った 子供の VM を ContentControl やその派生クラスなら インスタンスを持って管理し、 ItemsControl やその派生クラスなら ViewModel のコレクション(ObservableCollection など)を持って管理する。
親は、子供の VM の生成や消滅に責任を持たなければならない

より具体的には、Top Window の View や ViewModel は App.xaml.cs で生成するとして、Top Window がもつ、UserControl や Panel やその継承などの ViewModel は Top Window の ViewModel が生成しインスタンスを管理し、ItemsControl やその派生クラスなら ViewModel のコレクションを生成しインスタンスを管理する、ItemsControl の一行の View は DataTemplate DataType 構文で生成すると便利だ、Top Window の ViewModel が消滅するときや ItemsControl の一行の VM を削除するときは Dispose を行い子の VM に連鎖させる

Top Window VM の子供の VM も、Top Window と同じように子供を管理していくといい。

Top Window VM は シングルトン にしておくと便利だ、

ItemsControl の一行のアクティブな行の ViewModel は ActiveCustomerViewModel のようにコレクションからインスタンスをコピーしておくと扱いやすい。
よく使う VM は Top Window VM からショートカットを作っておくと使い勝手がよくなる。

このようにしておけば、どの VM からも目的の VM がたどれるようになり、VM から VM へのイベントが不要になる。

Top Window VM はシングルトンで ViewModel は Tree 構造で生成や消滅も管理することを推奨する。

投稿日時 : 2010年9月25日 1:03

コメントを追加

# Hello, Neat post. There's a problem with your website in web explorer, could test this? IE nonetheless is the market chief and a huge element of people will omit your fantastic writing because of this problem. 2019/04/01 8:06 Hello, Neat post. There's a problem with your webs

Hello, Neat post. There's a problem with your website in web explorer, could test this?
IE nonetheless is the market chief and a huge element of
people will omit your fantastic writing because of
this problem.

# Somebody necessarily lend a hand to make seriously articles I'd state. That is the first time I frequented your website page and up to now? I surprised with the research you made to create this particular put up amazing. Wonderful task! 2019/07/24 17:12 Somebody necessarily lend a hand to make seriously

Somebody necessarily lend a hand to make seriously articles
I'd state. That is the first time I frequented your website page and
up to now? I surprised with the research you made to
create this particular put up amazing. Wonderful task!

# I'm not sure exactly why but this blog is loading very slow for me. Is anyone else having this problem or is it a issue on my end? I'll check back later on and see if the problem still exists. 2019/08/13 22:05 I'm not sure exactly why but this blog is loading

I'm not sure exactly why but this blog is loading very slow for me.
Is anyone else having this problem or is it a issue on my
end? I'll check back later on and see if the problem still exists.

# I am curious to find out what blog system you happen to be working with? I'm experiencing some minor security problems with my latest website and I would like to find something more safe. Do you have any recommendations? 2019/09/17 0:40 I am curious to find out what blog system you happ

I am curious to find out what blog system you happen to be working with?
I'm experiencing some minor security problems with my latest website and I would like to find something more safe.

Do you have any recommendations?

タイトル
名前
URL
コメント