none
Dataset dynamical relations possible? Binding to a Tree. RRS feed

  • Question

  • Hi People,

    I want a TreeView with 3 Levels, Stations -> Substations -> Peripherals. I already have a Tree that is bound to a Dataset with a Master-Slave relation for Station -> Substations. The Dataset gets its data via SQL querie.

    The Problem I have now is adding the Peripherals-Level because it is very well possible that there are no Substations and the Peripherals are directly on the Mainstation. 

    So I need a Tree that can present Station -> Substation -> Peripherals   OR  Station -> Peripherals if there are no Substations, and I don`t really know how to realize that.

    This is what I have at the moment (for Stations -> Substations)

    DataTable dtStations = new DataTable("Stations"); DataTable dtSubstations = new DataTable("Substations"); DataSet ds = new DataSet(); .... try { DataColumn master = ds.Tables["Stations"].Columns["name"]; DataColumn child = ds.Tables["Substations"].Columns["mainstation"]; this.DataContext = this; ds.Relations.Add("ParentChild", master, child); }

    ...

    <TreeView x:Name="StationList" 
                                  Grid.Column="0" 
                                  Grid.Row="1"  
                                  HorizontalAlignment="Stretch" Margin="10,10,10,0"
                                  SelectedItemChanged="StationList_SelectedItemChanged_1" FontSize="16">
                            <TreeView.ItemTemplate>
                                <HierarchicalDataTemplate ItemsSource="{Binding ParentChild}">
                                    <TextBlock Text="{Binding name}" Padding="2" ToolTip="{Binding description}" />
                                    <HierarchicalDataTemplate.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding name}" Padding="2" ToolTip="{Binding description}" />
                                        </DataTemplate>
                                    </HierarchicalDataTemplate.ItemTemplate>
                                </HierarchicalDataTemplate>
                            </TreeView.ItemTemplate>
                        </TreeView>


    Thank you :)
    Friday, April 4, 2014 1:25 AM

Answers

  • Please forgive me if I do not understand your problem correctly.  In the past I found it easiest to work with hierarchical data as shown below.  Using this approach you can have hierarchies of varying depths. This will solve your problem of "what if there is no sub-station".

    Start with building your table correctly.  Your table should look like this:

    ID --- Primary Key

    ParentID (Nullable, null top level is null because there is no parent)

    Other columns as needed...

    You only need ONE table that will hold all your stations, sub-stations, and peripherals.  The ParentID of a sub-station is the ID of the parent station.  The ParentID of a peripheral is the ID of the sub-station (or station if no sub-station).  Dont use a separate table for each level in the hierarchy.  You may want to make ParentID a foreign key on the table (it points to itself).


    Now create a class that looks like this:

    class ThingHierarchy
    {
     int ID
     string Name and other properties as needed
     List<ThingHierarchy> Children
    }

    Now you need to write a recursive function that gets a parent and all of its children.  You will instatiate a list and call your "build" function like this:

    main()
    {
     List<ThingHierarchy> things = new List<ThingHierarchy>();
     BuildHierarchy(null, things);  // pass null on the first call to get objects with no parent - top level
     
     // work with entire hierarchy of things here
    }
    
    
    public void BuildHierarchy(int? id, List<ThingHierarchy> things)
    {
     ThingHierarchy thing = new ThingHierarchy();
     thing.ID = id;
     thing.Children = select * from db where ParentID = id; // gets all the sub-stations for a station, or peripherals for a sub-station
     things.add(thing);
     
     // now call this function recursively to get the children of each of the children of this item...................
     
     foreach(ThingHierarchy child in thing.Children)
      BuildHierarchy(child.ID, things);
     
    }



    • Edited by SWheaties Monday, April 7, 2014 6:27 AM
    • Marked as answer by Yang,Chenfei Friday, April 11, 2014 1:54 AM
    Monday, April 7, 2014 6:21 AM

All replies

  • You have to use hierarchical data templates.
    Monday, April 7, 2014 5:45 AM
  • Please forgive me if I do not understand your problem correctly.  In the past I found it easiest to work with hierarchical data as shown below.  Using this approach you can have hierarchies of varying depths. This will solve your problem of "what if there is no sub-station".

    Start with building your table correctly.  Your table should look like this:

    ID --- Primary Key

    ParentID (Nullable, null top level is null because there is no parent)

    Other columns as needed...

    You only need ONE table that will hold all your stations, sub-stations, and peripherals.  The ParentID of a sub-station is the ID of the parent station.  The ParentID of a peripheral is the ID of the sub-station (or station if no sub-station).  Dont use a separate table for each level in the hierarchy.  You may want to make ParentID a foreign key on the table (it points to itself).


    Now create a class that looks like this:

    class ThingHierarchy
    {
     int ID
     string Name and other properties as needed
     List<ThingHierarchy> Children
    }

    Now you need to write a recursive function that gets a parent and all of its children.  You will instatiate a list and call your "build" function like this:

    main()
    {
     List<ThingHierarchy> things = new List<ThingHierarchy>();
     BuildHierarchy(null, things);  // pass null on the first call to get objects with no parent - top level
     
     // work with entire hierarchy of things here
    }
    
    
    public void BuildHierarchy(int? id, List<ThingHierarchy> things)
    {
     ThingHierarchy thing = new ThingHierarchy();
     thing.ID = id;
     thing.Children = select * from db where ParentID = id; // gets all the sub-stations for a station, or peripherals for a sub-station
     things.add(thing);
     
     // now call this function recursively to get the children of each of the children of this item...................
     
     foreach(ThingHierarchy child in thing.Children)
      BuildHierarchy(child.ID, things);
     
    }



    • Edited by SWheaties Monday, April 7, 2014 6:27 AM
    • Marked as answer by Yang,Chenfei Friday, April 11, 2014 1:54 AM
    Monday, April 7, 2014 6:21 AM