silverlight 中的树形菜单

第一步是创建一个类:用于存放数据 
 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, VehicleCollection, 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, VehicleCollection, treemodel);
                    RecursionAgecny(AgencyCollection, LineCollection, VehicleCollection, child, treemodel);
                }
            }
            else
            {
                return;
            }
        }
        //线路递归
        private void RecursionLine(ObservableCollection<DepthDataService.Line> LineCollection, ObservableCollection<DepthDataService.Vehicle> VehicleCollection, 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, VehicleCollection, 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,0,2.5" BorderThickness="1" Height="472" x:Name="partTree" ItemTemplate="{StaticResource NodeTemplate}"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值