private static void AddInitial(ObservableCollection <OrgRoleDTO> source, RoleSelector ms)
        {
            if (source != null && source.Count > 0)
            {
                MultiSelectItem <OrgRoleDTO> tempSelction = new MultiSelectItem <OrgRoleDTO>()
                {
                    Items = new ObservableCollection <OrgRoleDTO>()
                    {
                        source.First()
                    },
                    CurrentItem = source.First()
                };

                if (source.First().ChildRoles.Count > 0)
                {
                    ms.SetValue(SelectionListProperty, new ObservableCollection <MultiSelectItem <OrgRoleDTO> >()
                    {
                        tempSelction, new MultiSelectItem <OrgRoleDTO>()
                        {
                            IsButton = true
                        }
                    });;
                }
                else
                {
                    ms.SetValue(SelectionListProperty, new ObservableCollection <MultiSelectItem <OrgRoleDTO> >()
                    {
                        tempSelction
                    });;
                }
            }
        }
        private static void AddCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            RoleSelector ms = sender as RoleSelector;

            ms.ExecuteSelctionChangedCallBack = false;
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelecItems = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;
            OrgRoleDTO LastItem = tempSelecItems[tempSelecItems.Count - 2].CurrentItem;

            if (LastItem != null)
            {
                MultiSelectItem <OrgRoleDTO> tempSelction = new MultiSelectItem <OrgRoleDTO>()
                {
                    Items       = new ObservableCollection <OrgRoleDTO>(LastItem.ChildRoles),
                    CurrentItem = LastItem.ChildRoles.First()
                };
                tempSelecItems.Insert(tempSelecItems.Count - 1, tempSelction);
                ms.SetValue(SelectionProperty, LastItem.ChildRoles.First());
                if (tempSelction.CurrentItem.ChildRoles.Count == 0)
                {
                    tempSelecItems.RemoveAt(tempSelecItems.Count - 1);
                }
                ms.SetValue(SelectionListProperty, tempSelecItems);
            }
            ms.ExecuteSelctionChangedCallBack = true;
        }
        private static void RemoveCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            RoleSelector ms = sender as RoleSelector;

            ms.ExecuteSelctionChangedCallBack = false;
            OrgRoleDTO param = e.Parameter as OrgRoleDTO;

            if (param.ParentRoleId == 0)
            {
                return;
            }
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelecItems = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;
            var removedItem      = tempSelecItems.ToList().Where(item => item.CurrentItem.Id == param.Id).First();
            int removedItemIndex = tempSelecItems.IndexOf(removedItem);

            for (int i = removedItemIndex; i < tempSelecItems.Count;)
            {
                tempSelecItems.RemoveAt(i);
            }
            ms.SetValue(SelectionProperty, tempSelecItems.Last().CurrentItem);
            if (tempSelecItems.Last().CurrentItem.ChildRoles.Count > 0)
            {
                tempSelecItems.Add(new MultiSelectItem <OrgRoleDTO>()
                {
                    IsButton = true
                });
            }
            ms.SetValue(SelectionListProperty, tempSelecItems);
            ms.ExecuteSelctionChangedCallBack = true;
        }
        private static void SelectionChangedCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            RoleSelector ms = sender as RoleSelector;

            if (ms.IsHandlingSelectionChangedCmd)
            {
                return;
            }
            ms.ExecuteSelctionChangedCallBack = false;
            ms.IsHandlingSelectionChangedCmd  = true;
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelecItems = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;
            OrgRoleDTO cmdParam         = e.Parameter as OrgRoleDTO;
            var        removedItem      = tempSelecItems.ToList().Where(item => item.Items.Contains(cmdParam)).First();
            int        removedItemIndex = tempSelecItems.IndexOf(removedItem);

            for (int i = removedItemIndex + 1; i < tempSelecItems.Count;)
            {
                tempSelecItems.RemoveAt(i);
            }

            if (cmdParam.ChildRoles.Count == 0)
            {
                if (tempSelecItems.Last().IsButton)
                {
                    tempSelecItems.RemoveAt(tempSelecItems.Count - 1);
                }
            }
            else
            {
                if (!tempSelecItems.Last().IsButton)
                {
                    tempSelecItems.Add(new MultiSelectItem <OrgRoleDTO>()
                    {
                        IsButton = true
                    });
                }
            }
            ms.SetValue(SelectionProperty, cmdParam);
            ms.SetValue(SelectionListProperty, tempSelecItems);

            ms.ExecuteSelctionChangedCallBack = true;
            ms.IsHandlingSelectionChangedCmd  = false;
            //Selection
        }
        private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RoleSelector ms = d as RoleSelector;

            if (e.NewValue == null)
            {
                return;
            }

            AddInitial(e.NewValue as ObservableCollection <OrgRoleDTO>, ms);
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelecItems = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;

            ms.ExecuteSelctionChangedCallBack = true;
            if (tempSelecItems == null)
            {
                return;
            }
            ms.SetValue(SelectionProperty, tempSelecItems[0].CurrentItem);
        }
        private static void SelectionChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RoleSelector ms = d as RoleSelector;

            if (!ms.ExecuteSelctionChangedCallBack)
            {
                var x = ms.GetValue(SelectionProperty);
                ms.ExecuteSelctionChangedCallBack = true;
                return;
            }

            OrgRoleDTO newRole = e.NewValue as OrgRoleDTO;
            ObservableCollection <OrgRoleDTO> sourceItems = ms.GetValue(SourceProperty) as ObservableCollection <OrgRoleDTO>;

            if (sourceItems == null)
            {
                return;
            }
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelctionList = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;

            if (tempSelctionList != null)
            {
                tempSelctionList.Clear();
            }



            if (newRole == null || newRole.Id == 0)
            {
                AddInitial(sourceItems, ms);
                return;
            }

            OrgRoleDTO currentParentValue = e.NewValue as OrgRoleDTO;
            //Clearing current selection list
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelectnList = ms.GetValue(SelectionListProperty) as ObservableCollection <MultiSelectItem <OrgRoleDTO> >;

            if (tempSelectnList != null)
            {
                tempSelectnList.Clear();
            }
            //Fetching Orginal list

            currentParentValue = (from role in sourceItems where role.Id == currentParentValue.Id select role).FirstOrDefault();
            ObservableCollection <MultiSelectItem <OrgRoleDTO> > tempSelecItems = new  ObservableCollection <MultiSelectItem <OrgRoleDTO> >();

            while (currentParentValue != null)
            {
                List <OrgRoleDTO> currentParentSiblings = (from role in sourceItems where role.ParentRoleId == currentParentValue.ParentRoleId select role).ToList();
                tempSelecItems.Add(new MultiSelectItem <OrgRoleDTO>()
                {
                    CurrentItem = currentParentValue, IsButton = false, Items = new ObservableCollection <OrgRoleDTO>(currentParentSiblings)
                });
                currentParentValue = (from role in sourceItems where role.Id == currentParentValue.ParentRoleId select role).FirstOrDefault();
            }
            tempSelecItems = new ObservableCollection <MultiSelectItem <OrgRoleDTO> > (tempSelecItems.Reverse());
            if (tempSelecItems.Last().CurrentItem.ChildRoles.Count > 0)
            {
                tempSelecItems.Add(new MultiSelectItem <OrgRoleDTO>()
                {
                    IsButton = true
                });
            }

            ms.SetValue(SelectionListProperty, tempSelecItems);
        }