I am a newbie in MVVM/WPF and try to use MVVM design pattern. I refer this tutorial https://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode to work with treeview and database.
I know that I can change Treeview selected Item and refer on it I can change in database also. I don't know is that the right way to work with treeview and database.
For example, I have database
ID Name
1 A
2 B
I use MVVM to populate it in treeview
A
|- B
Now, when rename node B to C. I need to search id of B in database and rename it also. Is that the right way to work with my idea or is there any better way to achieve it?
I know we can using like this article Binding TreeView to DataSet But it not use MVVM and some MVVM's features can't be applied.
B should be in a collection which is a property of A. Hence a person A has a children collection and an instance of B is in that collection.
The hierarchical datatemplate will then give you B as a child of A.
When you rename A to AA or whatever then B is still in that child collection. No need to change anything.
Although you could use a dataset to access data I think entity framework would be simpler. EF can give you data already arranged in the hierarchy necessary - with a children property. A dataset instead has a relationship you need to work with and build the collection yourself.
Here's an example of a self referencing key:
In that article, each employee has a staff id. Manager Id references the staff Id of an employee's manager.
The way this sort of relationship is usually handled in a rdbms is with a self referencing table. Hence a table people will have an ID. Each entry will have a ParentID. This should be an auto generated integer. If you instead made it a meaningful key and used name then when someone changes name ( maybe gets married or divorced ) then the value of the foreign key would have to be changed. By using a non-natural and generated key this is not necessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.