views:

22

answers:

0

OK I got a project that has a application layer, web layer and a data layer shared by the other two layers.

Now I have one part that is Hierarchical data that the application has to be able to look over, edit and add new to.

So at the top is Company wich can have many Divisions and each division has Employees & Production Licenses (two types of children) and so on.

Now I represent the data in a TreeView and my Models each have properties that return the children.

So CompanyModel has a property that returns a Collection of DivisionModels wich it gets through a LINQ to sql. ( Lazy loading I guess because I'm hoping it doesn't have to fetch the data unless someone requests this. Might be wrong and he fetches it anyways when the treeview is generated).

Q1) Should the collection here be a ObservableCollection? I'm using the same model in the WebLayer, will observable collection be a bother there?

Now besides the TreeView I have a MasterDetail view with the option to edit the data. So the selected item in the TreeView get's displayed there. Now all the models inherit from IEditableObject so I raise the BeginEdit, CancelEdit and EndEdit methods when needed.

I also have new Buttons. So I can click new company and it adds a new company. That works fine and it gets selected and jumps into edit mode.

New Division is only active when a Company is selected ( to set wich company the division belongs to ) and here the problem starts. I don't know how to add the newcompany to the treeview. If I add it to the Division property of the Company that won't work because it doesn't have a setter I guess ( no error just doesn't do anything ). I know I could save it to the database with the foreign key to the parentCompany but I don't wan't do that because I wan't the ability to cancel the add before It goes to the database and also It wouldn't validata without some data.

Q2) How to add childs of a dynimically generated TreeViewItems into the TreeView.

private void newCompanyBtnClick(object sender, RoutedEventArgs e)
    {
        CompanyModel newCompany = new CompanyModel();
        newCompany.Name = "New Company";
        CompanyCollection.Add(newCompany);
        newCompany.IsSelected = true;
        editBtnClick(null, null);
    }

    private void newDivisionBtnClick(object sender, RoutedEventArgs e)
    {
        if (CompanyTree.SelectedItem is CompanyModel)
        {
            DivisionModel newDiv = new DivisionModel();
            newDiv.Name = "New Division";
            newDiv.CompanyCode = ((CompanyModel)CompanyTree.SelectedItem).Code;
            ((CompanyModel)CompanyTree.SelectedItem).Divisions.Add(newDiv);
            newDiv.IsSelected = true;
        }
    }

Is my whole process perhaps wrong. Should I wrap it all into a some other ViewObject or does the CollectionView thingy help me somehow?