Silverlight 使用RadTreeView動態繫結資料

暖楓無敵發表於2011-10-14

在使用Telerik公司的RadTreeView控制元件時,再非同步呼叫WebService時,關於巢狀迴圈呼叫導致的阻塞現象,後來使用一次獲取WS資料,載入後通過LINQ來分類操作,避免了此種情況。



        #region 點選縣名後,動態載入該縣的所有實時險情類別資訊及類別下的所有實時險情

        RadTreeViewItem selectedItem = null;
        //選中時動態繫結
        private void RadTreeView1_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTreeView source = sender as RadTreeView;
            selectedItem = source.SelectedItem as RadTreeViewItem;
            if (!selectedItem.HasItems)
            {
                getTreeSoapClient client = new getTreeSoapClient();

                //實時險情列表
                client.getSSQXListCompleted += new EventHandler<getSSQXListCompletedEventArgs>(client_getSSQXListCompleted);
                client.getSSQXListAsync(selectedItem.Header.ToString().Trim());
            }
        }

        //實時險情類別
        RadTreeViewItem targetTreeNode = null;
        //實時險情名稱
        void client_getSSQXListCompleted(object sender, getSSQXListCompletedEventArgs e)
        {
            IList<SSXQ> xqList = e.Result;
            List<string> type = xqList.Select(p => p.災情型別).Distinct().ToList();
            foreach (string s in type)
            {
                targetTreeNode = new RadTreeViewItem();
                targetTreeNode.Header = s;
                targetTreeNode.IsExpanded = true;
                selectedItem.Items.Add(targetTreeNode);
                IList<SSXQ> list = (from li in xqList
                                    where li.災情型別 == s
                                    select li
                                  ).ToList<SSXQ>();
                if (list.Count > 0)
                {
                    foreach (SSXQ xq in list)
                    {
                        RadTreeViewItem objTreeNode = new RadTreeViewItem();
                        objTreeNode.Header = xq.圖片標題;
                        objTreeNode.Tag = xq.OBJECTID;
                        objTreeNode.DataContext = xq;
                        objTreeNode.IsExpanded = true;
                        targetTreeNode.Items.Add(objTreeNode);
                    }
                }
            }

            //設定拖拽操作
            //objTreeNode.Drag(objTreeNode.Header, null, (s, ex) =>
            //{
            //    Point p = ex.GetPosition(this.container);
            //    if (p.X > 0 && p.X < this.container.Width && p.Y > 0 && p.Y < this.container.Height)
            //        this.container.Children.Add(new RadTreeViewItem() { Header = (s as ContentControl).Content.ToString() });
            //});
        }
        #endregion


相關文章