2017-07-31 49 views
0

我正在使用MVVM,它工作得很好,除了一件事,訪問父模型對象。如何在樹結構中訪問父對象

目標是直接訪問任何模型對象的父對象,但我無法找到一個適當的方法來做到這一點。

例如:

祖父母

---父母

---兒童

--- --- ---孫子

我有一個孩子的參考,但我必須檢查一些兒童的屬性,也許父母。

目前代碼運行通過所有更高級別的對象,直到父母的子女的孫子與我的孫子對象成功匹配,然後可以檢查屬性。 但是,在智能代碼和效率方面,這是一種令人厭惡的行爲,不管這是如何完成的,我不希望爲幸運匹配運行所有數據。這是當前imoplementation,其他一些部分是通過使用LINQ完成的。

var someChild = calledChild; 
    foreach (Grandparent gParent in mainViewModel.SelectedEnvironment.GrandParents) 
    { 
     foreach (Parent parent in gParent.Parents) 
     { 
      foreach (Child child in parent.Children) 
      { 
       if (child.A == calledChild.A) 
       { 
        // Match 
        System.Diagnostics.Debug.WriteLine("CalledChilds grandparent is " + gParent.Name); 
       } 
      } 
     } 
    } 

該模型是建立在類中,像這樣的定義:

public class Parent : ObservableObject 
{ 
    public const string NamePropertyName = "Name"; 
    private string _name; 

    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (_name == value) 
      { 
       return; 
      } 
      _name = value; 
      RaisePropertyChanged(NamePropertyName); 
     } 
    } 

    public const string ChildrenPropertyName = "Children"; 
    private ObservableCollection<Child> _children; 

    public ObservableCollection<Child> Children 
    { 
     get 
     { 
      return _children; 
     } 
     set 
     { 
      if (_children == value) 
      { 
       return; 
      } 
      _children = value; 
      RaisePropertyChanged(ChildrenPropertyName); 
     } 
    } 
} 

該模型被保存在一個JSON文件,並解析回模型的根對象類型使用。

我不能只是添加一個新的引用「父」到「子」對象,因爲它會最終在一個循環中,由於這個概念的限制。

這將是很好的獲得引用,而不是整個模型分支的副本。

有沒有辦法直接訪問父對象?

謝謝大家!

+2

這不是一個真正的MVVM問題。當使用hirachical結構時,這是一個普遍問題。您可以在每個孩子中添加對父母的引用。如果你這樣做,你可以很容易地從父母訪問屬性。例如'GetParent()。GetParent()。property'當你必須從孫父母的存取屬性 –

+1

請提供一些示例代碼,它提供了更多關於你的問題的細節 – Fabiano

+0

歡迎!你的問題本質上是一組要求。任何代碼顯示? [問] – MickyD

回答

1

最簡單方法是存儲在子節點直接引用父節點:

public class ParentNode 
{ 
    private ObservableCollection<ChildNode> _children; 
    public ParentNode() 
    { 
     _children = new ObservableCollection<ChildNode>(); 
     Children = new ReadOnlyObservableCollection<ChildNode>(_children); 
    } 
    public ReadOnlyObservableCollection<ChildNode> Children { get; } 

    public void AddChild(ChildNode item) 
    { 
     if (item.Parent != null) throw new InvalidOperationException("Item is already added to another node"); 
     item.Parent = this; 
     _children.Add(item); 
    } 
    public void RemoveChild(ChildNode item) 
    { 
     if (item.Parent != this) throw new InvalidOperationException("Item is not direct child of this node"); 
     item.Parent = null; 
     _children.Remove(item); 
    } 
} 

public class ChildNode 
{ 
    public ParentNode Parent { get; internal set; } 
} 

只是要小心,因爲這將引入循環引用 - 父引用兒童,反之亦然。這有點違反DRY原則,因爲樹的形狀被定義了兩次,並且您可能很容易不同步(例如,您將ChildNode.Parent屬性設置爲除實際父級之外的其他屬性)。

有辦法解決它,但我認爲你可以從這開始。