This is a migrated thread and some comments may be shown as answers.

TreeView Self-reference hierarchy

5 Answers 121 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
jean michel
Top achievements
Rank 1
jean michel asked on 06 May 2010, 09:44 PM
Hello,

I Implemented this example populated from a database. But when inserting an item in the TreeView from an item of the ListBox DragAndDrop The TreeView is not updated. The list that loads the Treeview is of the type ObservableCollection

Loaded:
 protected void RadHierarquia_Loaded()  
        {  
            ObservableCollection<IHierarquico> hierarquia = new ObservableCollection<IHierarquico>();  
            HierarquicoCollection lHierarquia = new HierarquicoCollection();  
 
            foreach (IHierarquico item in hierarquia)  
            {  
                lHierarquia.Add(item);  
            }  
 
            RadHierarquia.DataContext = lHierarquia;  
        } 

ListBox to Treeview
 private void OnDropInfo(object sender, DragDropEventArgs e)  
        {  
 
            if (statusDrag == 1)  
            {  
                var destinationItem = e.Options.Destination as Telerik.Windows.Controls.RadTreeViewItem;  
                var papeis = meusPapeisBox.SelectedItems.Cast<DtoPapel>().ToList();//e.Options.Payload as IList<DtoPapel>;  
 
                if (papeis == null && e.Options.Payload is DtoPapel)  
                {  
                    papeis = new List<DtoPapel>() { e.Options.Payload as DtoPapel };  
                }  
 
                if (e.Options.Status == DragStatus.DropComplete)  
                {  
                    if (destinationItem != null && papeis != null)  
                    {  
                        foreach (var item in papeis)  
                        {  
                            HierarquicoData hData = destinationItem.Item as HierarquicoData;  
                              
                            DtoHierarquiaEstrutura obHierarquia = new DtoHierarquiaEstrutura();  
                            obHierarquia.IdEstruturaPai = hData.Id;  
                            obHierarquia.IdAgrupamento = hData.IdPai;  
                            obHierarquia.Papel = item;  
 
                            lHierarquia.Add(obHierarquia);  
 
                            destinationItem.IsExpanded = true;  
 
                            statusDrag = 0;  
 
 
                        }  
 
                        //foreach (Telerik.Windows.Controls.RadTreeViewItem itens in RadHierarquia.Items)  
                        //{  
                        //    LoadTreeView(itens);  
                        //}  
 
 
                        e.Handled = true;  
 
                    }  
                }  
            }  
            else  
            {  
                return;  
            }  
 
 
        } 

Thanks.

5 Answers, 1 is accepted

Sort by
0
Bobi
Telerik team
answered on 12 May 2010, 10:08 AM
Hello jean michel,

Is it possible to send us some sample code or explain in more details the structure of your application in order to reproduce and fix this issue?

Best wishes,
Bobi
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
jean michel
Top achievements
Rank 1
answered on 12 May 2010, 03:16 PM
Hello Bobi,

The structure is the following one: The HierarquicoData class inherits the IHierarquico interface of the DtoEstruturaHierarquica class. The HierarquicoCollection class inserts in the list objects of the HierarquicoData type. E the HierarchyConverter creates the structure of the tree.

HierarquicoData:
public class HierarquicoData : INotifyPropertyChanged, IComparable<HierarquicoData>, IEquatable<HierarquicoData> 
    {  
 
        readonly IHierarquico _item;  
 
        public HierarquicoData(IHierarquico data)  
        {  
            _item = data;  
        }  
 
        public HierarquicoData()  
        {  
        }  
 
        public IHierarquico Decorated  
        {  
            get { return _item; }              
        }  
 
        public HierarquicoCollection Owner  
        {  
            get;  
            set;  
        }  
 
        public string Descricao  
        {  
            get { return _item.Descricao; }  
        }  
 
        public string Imagem  
        {  
            get { return _item.Imagem; }  
        }  
 
        #region INotifyPropertyChanged Members  
 
        public event PropertyChangedEventHandler PropertyChanged;  
 
        protected virtual void OnPropertyChanged(string propertyName)  
        {  
            if (!string.IsNullOrEmpty(propertyName) &&  
                PropertyChanged != null)  
            {  
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
            }  
        }  
 
        #endregion  

DtoHierarquicoEstrutura
 public class DtoHierarquiaEstrutura : DtoBase, IIdentificavel<DtoPapel>, IIdentificavel<DtoHierarquiaAgrupamento>, IHierarquico, INotifyPropertyChanged  
    {  
        public event PropertyChangedEventHandler PropertyChanged;  
 
        private Boolean _ativo;  
        private Int32 _exclusao;  
        private Int32? _idEstruturaPai;  
        private DtoPapel _papel;  
        private Int32? _idAgrupamento;  
          
        public Boolean Ativo  
        {  
            get  
            {  
                return this._ativo;  
            }  
            set  
            {  
                if (this._ativo != value)  
                {  
                    this._ativo = value;  
                    OnPropertyChanged("Ativo");  
                }  
            }  
        }  
        public Int32 Exclusao  
        {  
            get  
            {  
                return this._exclusao;  
            }  
            set  
            {  
                if (this._exclusao != value)  
                {  
                    this._exclusao = value;  
                    OnPropertyChanged("Exclusao");  
                }  
            }  
        }  
          
      
        public Int32? IdAgrupamento  
        {  
            get  
            {  
                return this._idAgrupamento;  
            }  
            set  
            {  
                if (this._idAgrupamento != value)  
                {  
                    this._idAgrupamento = value;  
                    OnPropertyChanged("IdAgrupamento");  
                }  
            }  
        }  
 
        public DtoPapel Papel  
        {  
            get  
            {  
                return this._papel;  
            }  
            set  
            {  
                if (this._papel != value)  
                {  
                    this._papel = value;  
                    OnPropertyChanged("Papel");  
                }  
            }  
        }  
 
        public Int32? IdEstruturaPai  
        {  
            get  
            {  
                return this._idEstruturaPai;  
            }  
            set  
            {  
                if (this._idEstruturaPai != value)  
                {  
                    this._idEstruturaPai = value;  
                    OnPropertyChanged("CodEstruturaPai");  
                }  
            }  
        }  
 
       
        int IIdentificavel<DtoPapel>.Id  
        {  
            get  
            {  
                return Id;  
            }  
        }  
 
        int IIdentificavel<DtoHierarquiaAgrupamento>.Id  
        {  
            get  
            {  
                return Id;  
            }  
        }  
 
          
 
        protected virtual void OnPropertyChanged(string propertyName)  
        {  
            if (!string.IsNullOrEmpty(propertyName) &&  
                PropertyChanged != null)  
            {  
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
            }  
        }  
 
        #region IHierarquico Members  
 
        int IHierarquico.Id  
        {  
            get { return Id; }  
        }  
 
        int? IHierarquico.IdPai  
        {  
            get { return this.IdEstruturaPai; }  
        }  
 
        int? IHierarquico.IdAgrupamento  
        {  
            get { return this.IdAgrupamento; }  
        }  
 
        string IHierarquico.Descricao  
        {  
            get { return Papel.Nome; }  
        }  
 
        string IHierarquico.Imagem  
        {  
            get { return Papel.DescricaoImagem; }  
        }  
 
         
        #endregion  
    } 

XAML.cs
public partial class EstruturaHierarquica : UserControl  
    {  
 
        #region Globais  
 
        ObservableCollection<DtoPapel> meusPapeis, todosIcones = new ObservableCollection<DtoPapel>();  
        ObservableCollection<IHierarquico> hierarquia = new ObservableCollection<IHierarquico>();  
        HierarquicoCollection lHierarquia = new HierarquicoCollection();  
        DtoPapel payload = new DtoPapel();  
        DtoPapel iconeSelecionado = new DtoPapel();  
        DtoPapel papelSelecionadoOrigem = new DtoPapel();  
        Brush listBoxDragPossible = new SolidColorBrush(Colors.Orange);  
        List<DtoPapel> auxiliar = new List<DtoPapel>();  
        string nomeAgrup, auxNomeAgrup = "Novo Agrupamento";  
        int num, statusRequest = 0, statusSalvar, statusDrag = 0;  
 
        #endregion  
 
        #region EstruturaHierarquica  
 
        public EstruturaHierarquica()  
        {  
            InitializeComponent();  
 
            //Abre request para a controller  
            OpenWebClient();  
 
            //Evento do botão direito do mouse  
            //Telerik.Windows.EventManager.RegisterClassHandler(typeof(Telerik.Windows.Controls.RadMenuItem), Telerik.Windows.Controls.RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClicked), true);  
 
            Telerik.Windows.Input.Mouse.AddMouseDownHandler(this, OnRightMouseButtonUp);  
 
            RadDragAndDropManager.AddDragQueryHandler(this.meusPapeisBox, OnDragQuery);  
            RadDragAndDropManager.AddDropInfoHandler(this.RadHierarquia, OnDropInfo);  
            TraduzirTreeView();  
 
        }  
  #region wcRetornaHierarquiaEstrutura_OpenReadCompleted  
        void wcRetornaHierarquiaEstrutura_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)  
        {  
 
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<DtoHierarquiaEstrutura>));  
            List<DtoHierarquiaEstrutura> hierarquicoEstrutura = new List<DtoHierarquiaEstrutura>(serializer.ReadObject(e.Result) as List<DtoHierarquiaEstrutura>);  
            hierarquicoEstrutura.ForEach(row => hierarquia.Add(row));  
 
            RadHierarquia_Loaded();  
 
 
        }  
        #endregion  
 
#region RadHierarquia_Loaded  
 
        protected void RadHierarquia_Loaded()  
        {  
            foreach (IHierarquico item in hierarquia)  
            {  
                HierarquicoData data = new HierarquicoData(item);  
                lHierarquia.Add(data);  
            }  
 
            RadHierarquia.DataContext = lHierarquia;  
        }  
 
        #endregion  
 
 #region OnDropInfo  
        private void OnDropInfo(object sender, DragDropEventArgs e)  
        {  
 
            if (statusDrag == 1)  
            {  
                var destinationItem = e.Options.Destination as Telerik.Windows.Controls.RadTreeViewItem;  
                var papeis = meusPapeisBox.SelectedItems.Cast<DtoPapel>().ToList();//e.Options.Payload as IList<DtoPapel>;  
                Telerik.Windows.Controls.DropPosition dropPosition = destinationItem.DropPosition;  
                int dropIndex = destinationItem.Index;  
 
                Telerik.Windows.Controls.ItemsControl parentControl = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(destinationItem);  
 
 
                if (papeis == null && e.Options.Payload is DtoPapel)  
                {  
                    papeis = new List<DtoPapel>() { e.Options.Payload as DtoPapel };  
                }  
 
                if (e.Options.Status == DragStatus.DropComplete)  
                {  
                    if (destinationItem != null && papeis != null)  
                    {  
                        foreach (var item in papeis)  
                        {  
 
                            HierarquicoData hData = destinationItem.Item as HierarquicoData;  
 
                            DtoHierarquiaEstrutura obHierarquia = new DtoHierarquiaEstrutura();  
                            obHierarquia.IdEstruturaPai = hData.Decorated.Id;  
                            obHierarquia.IdAgrupamento = hData.Decorated.Id;  
                            obHierarquia.Papel = item;  
 
 
                            hierarquia.Add(obHierarquia);  
 
                            HierarquicoData data = new HierarquicoData(obHierarquia);  
                            lHierarquia.Add(data);  
 
                            RadHierarquia.DataContext = lHierarquia;  
 
                            destinationItem.IsExpanded = true;  
 
                            statusDrag = 0;  
 
                        }  
              
 
                        //foreach (Telerik.Windows.Controls.RadTreeViewItem itens in RadHierarquia.Items)  
                        //{  
                        //    LoadTreeView(itens);  
                        //}  
 
                        e.Handled = true;  
 
                    }  
                }  
            }  
            else  
            {  
                return;  
            }  
 
 
        }  
        #endregion 


Obs:
I identified that when I set the RadHierarquia.DataContext property in the RadHierarquia_Loaded event is invoked the HierarquicoCollection class, however in the OnDropInfo event when I set the same property do not occur nothing.

0
Bobi
Telerik team
answered on 13 May 2010, 12:34 PM
Hello jean michel,

We provided a sample projects for your ticket : 304817.
Please take a look at the examples. I hope that they will be helpful.

Best wishes,
Bobi
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
jean michel
Top achievements
Rank 1
answered on 13 May 2010, 01:10 PM
Hello,

Where I make download of this project?
 Thanks!!!
0
Tina Stancheva
Telerik team
answered on 13 May 2010, 01:51 PM
Hi jean michel,

You can download the examples from here:http://www.telerik.com/community/forums/silverlight/treeview/treeview-and-listbox-draganddrop.aspx

Let us know how it goes.

All the best,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
jean michel
Top achievements
Rank 1
Answers by
Bobi
Telerik team
jean michel
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or