微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,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}"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐