Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to make an IDE-like program and need to bind some data to a WPF TreeView control, which can be found in System.Windows.Controls namespace.

Here's how my binding should look:

-Project-------------------
---Sections----------------
------Containers-----------
---------DataItems---------
------------Actions--------
---------------Expressions-
---Section-----------------
------Containers-----------
---------DataItems---------
------------Actions--------
---------------Expressions-

In basic terms, what I am trying to bind the Solution Explorer items (ie: Project items) like in Visual Studio using Solution Explorer or the Class Explorer.

Here's my WPF Xaml Code:
HTML
<TreeView ItemsSource="{Binding Project.Sections}" x:Name="ProjectTreeView" removed="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <!-- Section template -->
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Containers}">
                        <TextBlock Foreground="WhiteSmoke" Text="{Binding Name}" />

                        <!-- Container template -->
                        <HierarchicalDataTemplate.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Methods}">
                                <TextBlock Text="{Binding Name}" />

                                <!-- Method template -->
                                <HierarchicalDataTemplate.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding}" />
                                    </DataTemplate>
                                </HierarchicalDataTemplate.ItemTemplate>

                            </HierarchicalDataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>

            </TreeView>


Here's My Library for the Hierarchy Data Template

C#
public MainWindow()
        {
            InitializeComponent();
            Project = new UserProject();
            CurrentViewLayer = ViewLayer.Project_Top_Level;

            //WelcomeScreen wScreen = new WelcomeScreen();
            //Nullable<bool> wScreenResult = wScreen.ShowDialog();
            //if(wScreenResult == true)
            //{
                //Load Default C# Project Data

            //}
            Project.Sections.Add(new CoreCodeGeneration_V2.Section("MyApp"));
            Project.Sections[0].Classes.Add(new Class("Program", AccessLevel.Public));
            Project.Sections[0].Classes[0].Methods.Add(new Method("Main", AccessLevel.Public));
            Project.Sections.Add(new CoreCodeGeneration_V2.Section("Utilities"));
            Project.Sections[1].Classes.Add(new Class("Calc", AccessLevel.Public));

            this.DataContext = Project;
        }

public class BaseObject
    {
        public BaseObject()
        {
            Name = null;
        }
        public BaseObject(string name)
        {
            Name = name;
        }
        public string Name
        {
            get;
            set;
        }
    }
public enum ProgrammingLanguage
    {
        CSharp,
        CPlusPlus,
        Java,
        VisualBasic,
        JavaScript
    }
public class UserProject : BaseObject
    {
        public UserProject()
        {
            Name = null;
            Language = ProgrammingLanguage.CSharp;
            Sections = new ObservableCollection<Section>();
        }
        public UserProject(string name)
        {
            Name = name;
            Language = ProgrammingLanguage.CSharp;
            Sections = new ObservableCollection<Section>();
        }
        public UserProject(string name,ProgrammingLanguage defaultLanguage)
        {
            Name = name;
            Language = defaultLanguage;
            Sections = new ObservableCollection<Section>();
        }
        public UserProject(string name,ProgrammingLanguage defaultLanguage,ObservableCollection<Section> sections)
        {
            Name = name;
            Language = defaultLanguage;
            Sections = sections;
        }
        public ProgrammingLanguage Language { get; set; }
        public ObservableCollection<Section> Sections { get; set; }
    }
public enum AccessLevel
    {
        Public, Private, Protected, Internal
    }
public class Section : BaseObject
    {
        public Section()
        {
            Name = null;
            Classes = new ObservableCollection<Class>();
            Structs = new ObservableCollection<Struct>();
            Interfaces = new ObservableCollection<Interface>();
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name)
        {
            Name = name;
            Classes = new ObservableCollection<Class>();
            Structs = new ObservableCollection<Struct>();
            Interfaces = new ObservableCollection<Interface>();
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name,ObservableCollection<Class> classes)
        {
            Name = name;
            Classes = classes;
            Structs = new ObservableCollection<Struct>();
            Interfaces = new ObservableCollection<Interface>();
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name, ObservableCollection<Struct> structs)
        {
            Name = name;
            Classes = new ObservableCollection<Class>();
            Structs = structs;
            Interfaces = new ObservableCollection<Interface>();
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name, ObservableCollection<Interface> interfaces)
        {
            Name = name;
            Classes = new ObservableCollection<Class>();
            Structs = new ObservableCollection<Struct>();
            Interfaces = interfaces;
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name, ObservableCollection<Enum> enums)
        {
            Name = name;
            Classes = new ObservableCollection<Class>();
            Structs = new ObservableCollection<Struct>();
            Interfaces = new ObservableCollection<Interface>();
            Enums = enums;
        }
        public Section(string name, ObservableCollection<Class> classes, ObservableCollection<Struct> structs)
        {
            Name = name;
            Classes = classes;
            Structs = structs;
            Interfaces = new ObservableCollection<Interface>();
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name, ObservableCollection<Class> classes, ObservableCollection<Struct> structs,ObservableCollection<Interface> interfaces)
        {
            Name = name;
            Classes = classes;
            Structs = structs;
            Interfaces = interfaces;
            Enums = new ObservableCollection<Enum>();
        }
        public Section(string name, ObservableCollection<Class> classes, ObservableCollection<Struct> structs, ObservableCollection<Interface> interfaces,ObservableCollection<Enum> enums)
        {
            Name = name;
            Classes = classes;
            Structs = structs;
            Interfaces = interfaces;
            Enums = enums;
        }
        public ObservableCollection<object> GetAllContainers()
        {
            ObservableCollection<object> ret = new ObservableCollection<object>();
            foreach (Class cl in Classes)
                ret.Add(cl);
            foreach (Struct st in Structs)
                ret.Add(st);
            foreach (Interface itf in Interfaces)
                ret.Add(itf);
            foreach (Enum en in Enums)
                ret.Add(en);
            return ret;
        }
        public ObservableCollection<Class> Classes
        {
            get;
            set;
        }
        public ObservableCollection<Struct> Structs
        {
            get;
            set;
        }
        public ObservableCollection<Interface> Interfaces
        {
            get;
            set;
        }
        public ObservableCollection<Enum> Enums
        {
            get;
            set;
        }
    }
public abstract class Container : BaseObject
    {
        public Container()
        {
            Name = null;
            Access = AccessLevel.Public;
        }
        public Container(string name)
        {
            Name = name;
            Access = AccessLevel.Public;
        }
        public Container(string name,AccessLevel access)
        {
            Name = name;
            Access = access;
        }
        public AccessLevel Access
        {
            get;
            set;
        }
    }
public class Class : Container
    {
        public Class()
        {
            Name = null;
            Access = AccessLevel.Public;
            Fields = new ObservableCollection<Field>();
            Properties = new ObservableCollection<Property>();
            Methods = new ObservableCollection<Method>();
            Events = new ObservableCollection<Event>();
        }
        public Class(string name)
        {
            Name = name;
            Access = AccessLevel.Public;
            Fields = new ObservableCollection<Field>();
            Properties = new ObservableCollection<Property>();
            Methods = new ObservableCollection<Method>();
            Events = new ObservableCollection<Event>();
        }
        public Class(string name,AccessLevel access)
        {
            Name = name;
            Access = access;
            Fields = new ObservableCollection<Field>();
            Properties = new ObservableCollection<Property>();
            Methods = new ObservableCollection<Method>();
            Events = new ObservableCollection<Event>();
        }
        public ContainerType GetContainerType()
        {
            return ContainerType.Class;
        }
        public ObservableCollection<Field> Fields
        {
            get;
            set;
        }
        public ObservableCollection<Property> Properties
        {
            get;
            set;
        }
        public ObservableCollection<Method> Methods
        {
            get;
            set;
        }
        public ObservableCollection<Event> Events
        {
            get;
            set;
        }
    }
public class Method : Action
    {
        public Method()
        {
            Name = null;
        }
        public Method(string name)
        {
            Name = name;
        }
        public Method(string name,AccessLevel access)
        {
            Name = name;
            
        }
    }

The rest of the library code can be obtained if you really need it, otherwise I want to avoid posting it.

Please, I need all of the help I can get to get this issue resolved, thanks!
Posted
Updated 2-Feb-14 15:50pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900