概述第一步是创建一个类:用于存放数据 public class TreeModel : INotifyPropertyChanged { public string ID { get; set; } public string Name { get; set; } public int TypeID { get; set; }
第一步是创建一个类:用于存放数据 public class TreeModel : INotifyPropertyChanged { public string ID { get; set; } public string name { get; set; } public int TypeID { get; set; } public string ParentID { get; set; } private bool? _shouldInstall; public bool HasSubcomponents { get { return Childs.Count > 0; } } //是否允许Feature进行安置 public bool? ShouldInstall { get { return _shouldInstall; } set { if (value != _shouldInstall) { _shouldInstall = value; OnPropertyChanged("ShouldInstall"); } } } public Collection<TreeModel> Childs { get; set; } public TreeModel() { Childs = new Collection<TreeModel>(); ShouldInstall = false; } public event PropertyChangedEventHandler PropertyChanged; //实现接口INotifyPropertyChanged定义函数 private voID OnPropertyChanged(string propertyname) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler.Invoke(this,new PropertyChangedEventArgs(propertyname)); } } } 第二部 wcf 返回的数据中进行赋值 private voID clIEnt_GetAgencylineVehicleCompleted(object sender,GetAgencylineVehicleCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message); } else { if (e.Result != null) { AgencylineVehicle agencylineVehicle = e.Result; ObservableCollection<Agency> AgencyCollection = agencylineVehicle.Agency; ObservableCollection<DepthDataService.line> lineCollection = agencylineVehicle.line; ObservableCollection<DepthDataService.Vehicle> VehicleCollection = agencylineVehicle.Vehicle; Collection<TreeModel> treeItems = new ObservableCollection<TreeModel>(); var agency = AgencyCollection.Where(a => Convert.ToInt32(a.ParentID) == 0); foreach (var item in agency) { TreeModel treemodel = new TreeModel(); treemodel.ID = item.ID; treemodel.name = item.name; treemodel.ParentID = item.ParentID; treeItems.Add(treemodel); Recursionline(lineCollection,VehicleCollection,treemodel); RecursionAgecny(AgencyCollection,lineCollection,item,treemodel); } partTree.ItemsSource = treeItems; //进行绑定到treevIEw 数据源上 } } } //递归机构的数据 private voID RecursionAgecny(ObservableCollection<Agency> AgencyCollection,ObservableCollection<DepthDataService.line> lineCollection,ObservableCollection<DepthDataService.Vehicle> VehicleCollection,Agency item,TreeModel treeItems) { var s = AgencyCollection.Where(a => Convert.ToInt32(a.ParentID) == Convert.ToInt32(item.ID)); if (s != null) { foreach (var child in s) { TreeModel treemodel = new TreeModel(); treemodel.ID = child.ID; treemodel.name = child.name; treemodel.ParentID = child.ParentID; treemodel.TypeID = 0; treeItems.Childs.Add(treemodel); Recursionline(lineCollection,treemodel); RecursionAgecny(AgencyCollection,child,treemodel); } } else { return; } } //线路递归 private voID Recursionline(ObservableCollection<DepthDataService.line> lineCollection,TreeModel treeModel) { var s = lineCollection.Where(a => Convert.ToInt32(a.AgencyID) == Convert.ToInt32(treeModel.ID)); if (s != null) { foreach (var child in s) { TreeModel treemodel = new TreeModel(); treemodel.ID = child.ID; treemodel.name = child.name; treemodel.ParentID = child.AgencyID; treemodel.TypeID = 1; treeModel.Childs.Add(treemodel); RecursionVehicle(VehicleCollection,treemodel); Recursionline(lineCollection,treemodel); } } else { return; } }
前台界面是:
样式的抒写 <sdk:HIErarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Childs}"> <!--<CheckBox Content="{Binding name}"/>--> <!--<TextBlock Text="{Binding name}"></TextBlock>--> <StackPanel OrIEntation="Horizontal"> <CheckBox IsTabStop="False" IsThreeState="{Binding HasSubcomponents}" IsChecked="{Binding ShouldInstall,Mode=TwoWay}" Click="ItemCheckBox_Click" Content="{Binding name}" /> <!--<ContentPresenter Content="{Binding name}" />--> </StackPanel> </sdk:HIErarchicalDataTemplate>
<sdk:TreeVIEw margin="0,2.5,2.5" borderThickness="1" Height="472" x:name="partTree" ItemTemplate="{StaticResource NodeTemplate}"/> @H_467_301@ 总结
以上是内存溢出为你收集整理的silverlight 中的树形菜单全部内容,希望文章能够帮你解决silverlight 中的树形菜单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)