Delayed loading of Treeview data

Source: Internet
Author: User

When using Treeview, many people may encounter slow interface loading when there are too many data items and poor user experience. To solve this problem, here I want to use the method of loading subitems with latency, that is, the data items we display are not necessarily used by users. during initialization, we only load the root node data on the interface. When the root node is expanded, only the data of the root node subitem is loaded. the subitem data is not loaded, and so on, in this way, the interface initialization takes a long time to load a lot of data at a time.

I recently watched x female agents. I still feel that it is okay. The Founding of a country requires the sacrifice of many people, including many innocent people, one final combination of love is not a day or two, and each other has many difficulties. I think the most important thing is mutual trust. No matter what trust the other party has, it is very important, the facts always come to the fore one day, and the ending is also very good. There is no appetite, and finally we come together. Although many gun battles are fake, after all, it's time-consuming and you don't have to worry about it, however, there are a lot of scenes in the last episode of The explosion. It seems that it is about to end, and all the explosives that can be used are used.

At the same time, we have to concentrate on doing a job. When there are more tasks at the same time, our efficiency will decrease. Our brain is not multi-core and will not perform parallel computing, however, reasonable arrangements can also help us improve our work efficiency, rest, play, and concentrate on work at work.

When you are bored, when you are idle, and when you need to talk about your life, you will not be bored. In addition, you will be passionate about the expected life, such as waiting for the update of a TV series, such as the expected love.

Next I will go to the question: how to implement delayed data loading. Here I will re-define the treeviewitem item, define an event, and trigger the loading event when the treeviewitem is expanded. below is my code implementation, togglebutton in the treeviewitem monitored here click the event

Public class treeitembase: treeviewitem {static treeitembase () {defastystylekeyproperty. overridemetadata (typeof (treeitembase), new frameworkpropertymetadata (typeof (treeitembase);} public treeitembase () {This. expanded + = new routedeventhandler (treeitembase_expanded);} public event func <treeitembase, ienumerable <treeitembase> getchidren; togglebutton _ togglebutton = NULL; Public override voi D onapplytemplate () {base. onapplytemplate (); _ togglebutton = This. gettemplatechild ("expander") as togglebutton; _ togglebutton. click + = new routedeventhandler (treeitembase_expanded);} void treeitembase_expanded (Object sender, routedeventargs e) {var BTN = sender as togglebutton; If (this. getchidren! = NULL) {// todo: load the calculation every time, or load the calculation var Childs = This. getchidren (this); If (Childs. Count ()! = 0) {_ togglebutton. visibility = system. windows. visibility. visible; this. begininit (); this. items. clear (); Childs. tolist (). foreach (I => {This. items. add (I) ;}); this. endinit (); If (BTN! = NULL) {This. isexpanded = BTN. ischecked = true ;}} else // if no data exists, hide the button before the item {If (_ togglebutton! = NULL) {_ togglebutton. Visibility = system. Windows. Visibility. Collapsed ;}}}}

 

In addition, the template also needs to be modified, that is, ischecked is bound to the isexpanded attribute. Otherwise, double-clicking the treeviewitem to load the data event will not trigger
<Setter Property="Control.Template">        <Setter.Value>            <ControlTemplate TargetType="gl:TreeItemBase">                          <Grid>                    <Grid.ColumnDefinitions>                        <ColumnDefinition Width="Auto" MinWidth="19" />                        <ColumnDefinition Width="Auto" />                        <ColumnDefinition Width="*" />                    </Grid.ColumnDefinitions>                    <Grid.RowDefinitions>                        <RowDefinition Height="Auto" />                        <RowDefinition />                    </Grid.RowDefinitions>                        <ToggleButton IsChecked="{TemplateBinding IsExpanded}" ClickMode="Press" Name="Expander">

Add my test code

        IList<Item> data = new List<Item>();        public MainWindow()        {            InitializeComponent();            var group0 = new Item() { Id = Guid.NewGuid().ToString(), Header = "Root1" };            group0.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var group1 = new Item() { Id = Guid.NewGuid().ToString(), Header = "Root0" };            group1.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item1 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = group1.Id, Header = "Leaf1" };            item1.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item2 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = group1.Id, Header = "Leaf2" };            item2.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item3 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = group1.Id, Header = "Leaf3" };            item3.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item4 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = item3.Id, Header = "Leaf3:Leaf1" };            item4.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item5 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = item3.Id, Header = "Leaf3:Leaf2" };            item5.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            var item6 = new Item() { Id = Guid.NewGuid().ToString(), ParentId = item3.Id, Header = "Leaf3:Leaf3" };            item6.GetChidren += new Func<TreeItemBase, IEnumerable<TreeItemBase>>(group1_GetChidren);            data.Add(group1);            data.Add(item1);            data.Add(item2);            data.Add(item3);            data.Add(item4);            data.Add(item5);            data.Add(item6);            group0.Items.Add(group1);            treeView1.ItemsSource = group0.Items;        }        IEnumerable<TreeItemBase> group1_GetChidren(TreeItemBase arg)        {            return data.Where(e => e.ParentId == (arg as Item).Id);        }    public class Item : TreeItemBase    {        public string Id { get; set; }        public string ParentId { get; set; }    }

This is just a way of implementation. If you have better implementation methods or comments, please give us some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.