0
votes

I have a treeview setup with a HierarchicalDataTemplate in my XAML as follows:

<TreeView Grid.Column="1" Grid.Row="0" ItemsSource="{Binding treeViewObsCollection}">                    
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding mainNodes}">
                        <TextBlock Text="{Binding topNodeName}"/>
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>   
                                    <TextBlock Text="{Binding subItemName}"></TextBlock>      
                            </DataTemplate>                            
                        </HierarchicalDataTemplate.ItemTemplate>                      
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>                
            </TreeView>

Here is the ViewModel which is set as the Data Context for the XAML File.

class AdvancedErrorCalculationViewModel : ViewModelBase
    { 
        static ObservableCollection<TreeViewClass> _treeViewObsCollection = new ObservableCollection<TreeViewClass>();
        public static ObservableCollection<TreeViewClass> treeViewObsCollection { get { return _treeViewObsCollection; } }
            public class TreeViewClass : ViewModelBase
            {
            public TreeViewClass(string passedTopNodeName)
            {
                topNodeName = passedTopNodeName;
                mainNodes = new ObservableCollection<TreeViewSubItems>();
            }

            public string topNodeName
            {
                get { return this.GetValue<string>(); }
                set { this.SetValue(value); }
            }

            public ObservableCollection<TreeViewSubItems> mainNodes { get; private set; }
        }

        public class TreeViewSubItems : ViewModelBase
        {
            public TreeViewSubItems(string passedSubItemName, string passedSubItemJobStatus)
            {
                subItemName = passedSubItemName;
                subItemJobStatus = passedSubItemJobStatus;                
            }

            public string subItemName
            {
                get { return this.GetValue<string>(); }
                set { this.SetValue(value); }
            }

            public string subItemJobStatus
            {
                get { return this.GetValue<string>(); }
                set { this.SetValue(value); }
            }            
        }

        public static void AddTreeViewItems(string passedTopNodeName, string passedChildItemName, string passedChildItemJobStatus)
        {            
            treeViewObsCollection.Add(new TreeViewClass(passedTopNodeName)
            {
                mainNodes =
                {
                    new TreeViewSubItems(passedChildItemName, passedChildItemJobStatus)
                }
            });
        }
}

The problem lies with the AddTreeViewItems method seen above. Currently, if I call the method by let's say:

AdvancedErrorCalculationViewModel.AddTreeViewItems("Parent","Child 1", "Completed");

it creates the parent and child nodes just fine. No issues there.

However, if I call the method again with the same Parent name but a different Child; for example: AdvancedErrorCalculationViewModel.AddTreeViewItems("Parent","Child 2", "Completed");

It creates a new Parent node with a child node Child 2 instead. Thus, I have something like this:

- Parent
  - Child 1

- Parent
  - Child 2

I would like to modify this function so that if the parent name is the same, the child node should belong under the previous Parent instead of under a new Parent node. Something like this:

- Parent
  - Child 1
  - Child 2

I have tried to source for a solution on stackoverflow for the whole day but can't seem to find any that fits my problem. Could anyone help me modify my code to detect if the Parent node exists in the observablecollection already and if so, place the child node under the existing parent?

Thanks advance. :)

1

1 Answers

0
votes

Break it down into steps:

  1. Search treeViewObsCollection for a node with passedTopNodeName
  2. Create a new node if that fails
  3. Add the new TreeViewSubItems

You can use LINQ:

    public static void AddTreeViewItems(string passedTopNodeName, string passedChildItemName, string passedChildItemJobStatus)
    {
        TreeViewClass topNode = treeViewObsCollection.FirstOrDefault(x => x.topNodeName == passedTopNodeName);
        if (topNode == null)
        {
            topNode = new TreeViewClass(passedTopNodeName);
            treeViewObsCollection.Add(topNode);
        }
        topNode.mainNodes.Add(new TreeViewSubItems(passedChildItemName, passedChildItemJobStatus));
    }