How can I modify my classes to use it's collections in WPF TreeView

Posted by Victor on Stack Overflow See other posts from Stack Overflow or by Victor
Published on 2010-03-28T16:53:51Z Indexed on 2010/03/28 17:03 UTC
Read the original article Hit count: 285

Hello, i'am trying to modify my objects to make hierarchical collection model. I need help. My objects are Good and GoodCategory:

public class Good 
        {
            int _ID;
            int _GoodCategory;
            string _GoodtName;

            public int ID
            {
                get { return _ID; }
            }

            public int GoodCategory
            {
                get { return _GoodCategory; }
                set
                {
                    _GoodCategory = value;
                }
            }

            public string GoodName
            {
                get { return _GoodName; }
                set
                {
                    _GoodName = value;
                }
            }

            public Good(IDataRecord record)
            {
                _ID = (int)record["ID"];
                _GoodtCategory = (int)record["GoodCategory"];
            }
     }

    public class GoodCategory
    {
        int _ID;
        string _CategoryName;

        public int ID
        {
            get { return _ID; }
        }

        public string CategoryName
        {
            get { return _CategoryName; }
            set
            {
                _CategoryName = value;
            }
        }

        public GoodCategory(IDataRecord record)
        {
            _ID = (int)record["ID"];
            _CategoryName = (string)record["CategoryName"];
        }
    }

And I have two Collections of these objects:

public class GoodsList : ObservableCollection<Good>
        {
            public GoodsList()
            {
                string goodQuery = @"SELECT `ID`, `ProductCategory`, `ProductName`, `ProductFullName` FROM `products`;";

                using (MySqlConnection conn = ConnectToDatabase.OpenDatabase())
                {
                   if (conn != null)
                        {
                            MySqlCommand cmd = conn.CreateCommand();
                            cmd.CommandText = productQuery;

                            MySqlDataReader rdr = cmd.ExecuteReader();
                            while (rdr.Read())
                            {
                                Add(new Good(rdr));
                            }
                        }
                }
            }
        }

public class GoodCategoryList : ObservableCollection<GoodCategory>
        {
            public GoodCategoryList ()
            {
                string goodQuery = @"SELECT `ID`, `CategoryName` FROM `product_categoryes`;";

                using (MySqlConnection conn = ConnectToDatabase.OpenDatabase())
                {
                   if (conn != null)
                        {
                            MySqlCommand cmd = conn.CreateCommand();
                            cmd.CommandText = productQuery;

                            MySqlDataReader rdr = cmd.ExecuteReader();
                            while (rdr.Read())
                            {
                                Add(new GoodCategory(rdr));
                            }
                        }
                }
            }
        }

So I have two collections which takes data from the database. But I want to use thats collections in the WPF TreeView with HierarchicalDataTemplate. I saw many post's with examples of Hierarlichal Objects, but I steel don't know how to make my objects hierarchicaly. Please help.

© Stack Overflow or respective owner

Related posts about treeview

Related posts about class-hierarchy