オブジェクトを別の方法で2回提示する必要があります。
- TreeViewのノードとして(ナビゲーション/名前変更)
- 2つのTextBoxとして(コンテンツの名前変更/編集)
public class Item
{
public string Name{get;set;}
public string Content{get;set;}
}
私の最初の解決策は、物事をシンプルに保つことでした。
public class MainViewModel
{
// collection of items (treeview navigation)
public BindingList<ItemViewModel> Items{get;set;}
// selected item (from treeview navigation)
// used for textbox edit
public ItemViewModel SelectedItem{get;set;}
}
public class ItemViewModel
{
// Used for treeview navigation
public bool IsSelected{get;set;}
public bool IsExpanded{get;set;}
public bool IsInEditNameMode{get;set;}
public BindingList<ItemViewModel> Children{get;set;}
public void BuildChildren();
// Used for treeview display/rename
// Used for textbox display/rename
public string Name{get;set;}
// Used for textbox edit
public string Content{get;set;}
}
これはしばらくの間うまく機能します。しかし、アプリケーションがより複雑になるにつれて、ビューモデルはますます「汚染」されます。
たとえば、同じビューモデルにプレゼンテーションを追加する(高度なプロパティ、グラフ表現など)
public class ItemViewModel
{
// Used for Advanced properties
public BindingList<PropertyEntry> Properties {get;set;}
public PropertyEntry SelectedProperty{get;set;}
// Used for graph relationship
public BindingList<ItemViewModel> GraphSiblings{get;set;}
public bool IsGraphInEditNameMode{get;set;}
public bool IsSelectedGraphNode {get;set;}
public void BuildGraphSiblings();
// Used for treeview navigation
public bool IsNavigationInEditNameMode{get;set;}
public bool IsSelectedNavigationNode{get;set;}
public bool IsExpandedNavigationNode{get;set;}
public BindingList<ItemViewModel> NavigationChildren{get;set;}
public void BuildNavigationChildren();
// Used for treeview display/rename
// Used for textbox display/rename
// Used for graph display
// Used for Advanced properties display
public string Name{get;set;}
// Used for textbox edit
public string Content{get;set;}
}
現在、複数のプレゼンテーションに単一のビューモデルを使用しています。これは、選択したアイテムがすべてのプレゼンテーションで同期されているためです。
また、プロパティ(名前/コンテンツ)を複製し続ける必要はありません。
そして最後に、PropertyChanged通知は、アイテムのすべての表示を更新するのに役立ちます(つまり、ナビゲーションで名前を変更すると、TextBox / Graph / Advancedプロパティなどが更新されます)。
ただし、それはいくつかの原則(単一責任、最小特権など)の違反のようにも感じます。
しかし、同期/プロパティ通知を機能させ続ける/新しいビューモデルごとにモデルのプロパティを複製するなどの多くのコードを記述せずに、リファクタリングする方法がよくわかりません)
私が知りたいこと:
もしそれがあなた次第だったとしたら、あなたはこれをどのように解決したでしょうか?
現時点では、すべてがまだ機能しています。コードをさらに改善できると思います。それが私が助けを必要としていることです。