如何修改我的类以在 WPF TreeView 中使用它的集合

发布于 2024-08-27 06:10:10 字数 3212 浏览 10 评论 0原文

我正在尝试修改我的对象以制作分层集合模型。我需要帮助。我的对象是 Good 和 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"];
        }
    }

我有这些对象的两个集合:

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));
                            }
                        }
                }
            }
        }

所以我有两个从数据库获取数据的集合。但我想将 WPF TreeView 中的集合与 HierarchicalDataTemplate 一起使用。我看到很多帖子都有分层对象的示例,但我不知道如何使我的对象分层。请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-09-03 06:10:10

将集合属性添加到“父”类(我猜是 GoodCategory)。您可以将其设为 IListObservableCollection。 (或者,如果您不希望使用代码能够将 Goods 添加到 GoodCategory,请使用只读集合。)例如:

class GoodCategory
{
  private ObservableCollection<Good> _goods = new ObservableCollection<Good>();

  public ObservableCollection<Good> Goods
  {
    get { return _goods; }
  }
}

您需要确保该集合与 Good.GoodCategory 属性正确同步 - - 例如,当 Good.GoodCategory 属性更改时,Good 可能会将其自身从现有的 GoodCategory.Goods 集合中删除,并将其自身添加到新的 GoodCategory 的 Goods 集合中。如果您使用对象关系映射器而不是手工制作的类和 SQL 语句,那么 ORM 应该会为您处理这个问题。

Add a collection property to the "parent" class (I guess GoodCategory). You might make this an IList<ChildType> or an ObservableCollection<ChildType>. (Or, if you don't want consuming code to be able to add Goods to a GoodCategory, use a read-only collection.) For example:

class GoodCategory
{
  private ObservableCollection<Good> _goods = new ObservableCollection<Good>();

  public ObservableCollection<Good> Goods
  {
    get { return _goods; }
  }
}

You will need to ensure that this collection is properly synchronised with the Good.GoodCategory property -- for example, when the Good.GoodCategory property changes a Good might remove itself from its existing GoodCategory.Goods collection and add itself to the new GoodCategory's Goods collection. If you use an object-relational mapper rather than handcrafted classes and SQL statements than the ORM should take care of this for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文