silverlight如何实现<controls:TreeViewItem>控件绑定数据库中数据
silverlight如何实现<controls:TreeViewItem>控件绑定数据库中数据?急求代码 最好是现成的demo 数据库最好是sql
[解决办法]
其实网络上已经有了很多楼主需求的实例,Silverlight绑定Treeview分为两部分,
1. 客户端部分,treeview绑定
2. 数据库交互部分,负责SQL数据读取;
第一部分,你可以参考一下几篇教程,其中有 源代码下载:
http://www.silverlightchina.net/html/tips/2010/0510/1110.html
http://www.silverlightchina.net/html/tips/2010/0101/515.html
http://www.silverlightchina.net/html/tips/2010/0101/514.html
http://www.silverlightchina.net/html/tips/2010/0101/513.html
http://www.silverlightchina.net/html/tips/2009/1211/391.html
http://www.silverlightchina.net/html/developer/2009/1212/393.html
第二部分,你可以参考下面的教程,WCF与SQL的通讯,其中也有源代码查看:
图文详解Silverlight访问MSSQL数据库
http://silverlightchina.net/html/tips/2009/1229/503.html
http://silverlightchina.net/html/zhuantixilie/getstart/2009/1210/387.html
[解决办法]
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Navigation;using System.ServiceModel.DomainServices.Client;using ABISCustom.Web;namespace ABISAdmin{ public partial class temp : Page { DomainServiceCustom customContext = new DomainServiceCustom(); public t_sys_Group modelGroupCw = new t_sys_Group(); public List<t_sys_Group> listGroup = new List<t_sys_Group>(); public List<t_sys_Menu> listMenu = new List<t_sys_Menu>(); //public List<v_GroupMenuList> listVGM = new List<v_GroupMenuList>(); public temp() { InitializeComponent(); showTree(); } // 当用户导航到此页面时执行。 protected override void OnNavigatedTo(NavigationEventArgs e) { } void showTree() { //清除树的所有节点 tvMenu.Items.Clear(); listMenu.Clear(); tvMenu.DataContext = null; var query = (from menu in customContext.GetT_sys_MenuQuery() select menu); LoadOperation<t_sys_Menu> contact = customContext.Load(query); contact.Completed += new EventHandler(contact_Completed); } void contact_Completed(object sender, EventArgs e) { LoadOperation<t_sys_Menu> contact = (LoadOperation<t_sys_Menu>)sender; listMenu = (from menu in contact.Entities select menu).AsQueryable().ToList(); AddTreeNode("ZZZZ", null); //指定选择项开始========================================= TreeViewItem tr = new TreeViewItem(); tr = tvMenu.Items[0] as TreeViewItem; tr.IsSelected = true; //指定选择项结束========================================= } private void AddTreeNode(string parentID, TreeViewItem treeViewItemTemp) { List<t_sys_Menu> result = (from menuInfo in listMenu where menuInfo.PMenuCode == parentID select menuInfo).ToList<t_sys_Menu>(); if (result.Count > 0) { foreach (t_sys_Menu menuinfo in result) { TreeViewItem objTreeNode = new TreeViewItem(); objTreeNode.Header = menuinfo.MenuName; objTreeNode.DataContext = menuinfo; objTreeNode.IsExpanded = true; if (treeViewItemTemp == null) { tvMenu.Items.Add(objTreeNode); } else { treeViewItemTemp.Items.Add(objTreeNode); } AddTreeNode(menuinfo.MenuCode, objTreeNode); } } } }}
[解决办法]
if exists (select * from sysobjects where id = OBJECT_ID('[t_sys_Menu]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) DROP TABLE [t_sys_Menu]CREATE TABLE [t_sys_Menu] ([ID] [uniqueidentifier] NULL DEFAULT (newid()),[MenuCode] [varchar] (100) NOT NULL,[MenuName] [nvarchar] (200) NULL,[PMenuCode] [varchar] (100) NULL,[FileName] [varchar] (200) NULL,[FileExt] [varchar] (200) NULL,[Ico] [varchar] (200) NULL,[ImgIndex] [int] NULL DEFAULT ((-1)),[DisplayOrder] [varchar] (100) NULL,[IsExpand] [char] (1) NULL DEFAULT (1),[IsUse] [char] (1) NULL DEFAULT (1))ALTER TABLE [t_sys_Menu] WITH NOCHECK ADD CONSTRAINT [PK_t_sys_Menu] PRIMARY KEY NONCLUSTERED ( [MenuCode] )INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( '894e5592-856c-4273-84f3-ba5e39c1e627','0','根节点','ZZZZ','folder.png',-1,'1','1')INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( 'ccce25bb-7965-4af4-88a6-43ccdb673d55','10','公共查询','0','flag_pink.png',1,'0','1')INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[FileName],[FileExt],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( '1d6cbc07-da29-4989-86ad-efda00db3a7c','1001','查询1','10','de','xap','sport_8ball.png',-1,'1','1')INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[FileName],[FileExt],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( '8713a1c6-99d7-46cd-b114-0af56faec5db','1002','查询2','10','111','xap','male.png',-1,'1','1')INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[FileExt],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( '9878e06e-1e83-4366-b7d9-65a85ff27234','20','文件输入','0','xap','male.png',1,'1','1')INSERT [t_sys_Menu] ([ID],[MenuCode],[MenuName],[PMenuCode],[Ico],[ImgIndex],[IsExpand],[IsUse]) VALUES ( '9bb78503-b8b2-4c11-b717-792bccb716df','99','系统设置','0','sport_8ball.png',1,'0','1')