The Course Design of the operating system is to build a file system. I chose to use Silverlight to make the file operating system interface, because I plan to use this interface on the OJ and team websites. In the file operating system interface, the tree file structure is naturally indispensable. The first effect that this control reminds me is, of course, the dynamic loading of the file structure, without a doubt, this is a good combination of user experience and software performance. Effect participants:
Click the interface before the directory:
Click the directory after the Catalog0-2 Interface:
Careful readers can find that, in the click before the Catalog0-2 directory item in front of the Small arrow icon, click after the directory has, this is a dynamic load of a proof. Due to space issues, you cannot provide completeCode. The following describes several ways to achieve dynamic loading:
1) dynamically Add sub-items using the items attribute:
Each subitem in the Treeview is of the treeviewitem type (Catalog0-0 In the example ~ Catalog0-9 ). The treeviewitem type is inherited from headereditemscontrol (the reader can ignore the details of this type first), while the latter inherits from itemscontrol. This itemscontrol class has an items attribute, through which the collection is returned, you can add or delete a subitem:
Treeviewitem sampleitem = new treeviewitem ();
Sampleitem. Items. Add (new object ());
Furthermore, Treeview is inherited from itemscontrol, which has a selecteditemchanged event. As the name suggests, this event is triggered when a subitem is selected on the interface (note that the trigger is a parent item ).
Therefore, the first way to dynamically load sub-directories is:
A. Capture the selecteditemchanged event of the Treeview;
B. In the event processing code, call the selecteditem attribute of the Treeview to obtain the selected
Treeviewitem; (example in the figure, the selected is the treeviewitem Catalog0-2)
C. Call the items attribute of the selected treeviewitem to add new sub-items for dynamic loading.
Key Code: (this code is not an effective code, but a code for this implementation method)
Tree control XAML:
Code
< Controls: Treeview X: Name = "Testinstance"
Selecteditemchanged = "Onselecteditemchanged" >
< Controls: treeviewitem Header = "Catalog0-0" />
< Controls: treeviewitem Header = "Catalog0-1" />
< Controls: treeviewitem Header = "Catalog0-2" />
< Controls: treeviewitem Header = "Catalog0-3" />
< Controls: treeviewitem Header = "Catalog0-4" />
< Controls: treeviewitem Header = "Catalog0-5" />
< Controls: treeviewitem Header = "Catalog0-6" />
< Controls: treeviewitem Header = "Catalog0-7" />
< Controls: treeviewitem Header = "Catalog0-8" />
< Controls: treeviewitem Header = "Catalog0-9" />
</ Controls: Treeview >
Event ProcessingProgramCode:
Code
Private Void Onselecteditemchanged ( Object Sender, routedpropertychangedeventargs < Object > E)
{
Treeview Source = Sender As Treeview;
Treeviewitem selecteditem = Source. selecteditem As Treeviewitem;
For ( Int I = 0 ; I < 5 ; I ++ )
{
Selecteditem. Items. Add ( New Treeviewitem () {Header = Selecteditem. Header + " - " + I });
}
}
2) bind the subitem data source through itemssource:
The itemscontrol mentioned above also has an important attribute, itemssource. When an object inherited from ienumerable or ienumerable <t> is set to this attribute, itemscontrol automatically binds all objects in the enumerated set to its own subitem. The above event handler code can be replaced with itemssource as follows:
Code
Private Void Onselecteditemchanged ( Object Sender, routedpropertychangedeventargs < Object > E)
{
Treeview Source = Sender As Treeview;
Treeviewitem selecteditem = Source. selecteditem As Treeviewitem;
Ilist < Treeviewitem > Newitems = New List < Treeviewitem > ();
For ( Int I = 0 ; I < 5 ; I ++ )
{
Newitems. Add ( New Treeviewitem () {Header = Selecteditem. Header + " - " + I });
}
Selecteditem. itemssource=Newitems;
}
In this way, you can enjoy many. NET data binding functions. However, once itemsscource is used to bind the subitem data source, the subitem set returned by the items attribute will be a read-only set, any operation that modifies the set content, such as add and remove, called by the set will trigger an exception, which means the two methods are mutually exclusive.
In the above code, I only registered the selecteditemchanged event handler for the root Treeview control, but the treeviewitem added to the subitem will automatically use the event handler, And the subitem will also, this recursion. Readers who test the above Code will find the effect interesting.
When the observablecollection <t> class is used as the data source binding, the effect is better, because all operations on the data source set are automatically updated to the tree control view. After a general set is bound, operations on the original set are not automatically updated to the tree control view. The manual update method is to reset the itemsscource attribute. Although you still use the same data source set, the documentation on msdn has been described, this method is dozens of times slower than the observablecollection method.ArticleLink: http://msdn.microsoft.com/zh-cn/library/bb613546.aspx.