StackTrace
StackTrace

Reputation: 9416

radTreeListView - allow selection of items only under same rootItem

how can make the telerik radTreeListView only allow selection of items under the same rootItem/ParentItem?

 UK
  -london
      -london 1
      -london --
      -london 100
  -Newcastle
      -Newcastle 1
      -Newcastle 2
      -Newcastle 3
  -liverpool
USA
  -New york
  -califonia
China
  -one
  -two

From above sample menu of a radTreeListView, user should be allowed to select only

UK, USA or China. OR London, Newcastle, Liverpool, OR Newcastle 1, Newcastle 2, Newcastle 3 BUT NOT Newcastle 1, London 1, UK ALSO NOT UK, Newcastle, Newcastle 1.

User should only select things/items at the same hierarchy. Basically, they should not select a parent and a child, THEY either select from parents or children or sub-children.

This is the control am using : http://www.telerik.com/help/silverlight/radtreelistview-overview.html

Upvotes: 0

Views: 325

Answers (1)

saeed
saeed

Reputation: 2497

Well, here you should do it manually if you populate your data in class and set a parent for each data you can check who is the selected node parent!

So now how to populate data?

Try this method more common and also a sample has been provided in Telerik's site!

I try your sample data as following try this and enjoy SO for learning new things.

Create a class that contains a parent

See this class:

 public class Category
    {
        public string Name { get; set; }
        public Category Parent { get; set; }
        public ObservableCollection<Category> Items{get;set;}
        //Constructor
        public Category(string Cat_name, Category Cat_parent)
        {
            Name = Cat_name;
            Parent = Cat_parent;
            Items = new ObservableCollection<Category>();
        }
        /// <summary>
        /// Adds a child to this node
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public void AddChild(Category child)
        {
            if (child == null) //check if the child is not null
                return;
            Items.Add(child);
        }
        /// <summary>
        /// Returns Root Parent
        /// </summary>
        /// <returns></returns>
        public Category RootPanel()
        {
            if (Parent == null)
            {
                return this;
            }
            else
            {
                return this.Parent.RootPanel();
            }
        }
    }

Now i try to generate your sample data "UK-USA-China"

here is the code which generate your sample data

  public static ObservableCollection<Category> GetCategoryData()
    {
        ObservableCollection<Category> data = new ObservableCollection<Category>();
        Category UK = new Category("UK", null);//Because it's root panel it has no parents so parent should set to null
        //Adding sub category of UK
        Category London = new Category("London", UK);
        UK.AddChild(London);
        Category NewCastel = new Category("NewCastel", UK);
        UK.AddChild(NewCastel);
        Category LiverPool = new Category("LiverPool", UK);
        UK.AddChild(LiverPool);
        //adding childs of London and  Newcastle
        for (int i = 1; i <= 100; i++)
        {
            London.AddChild(new Category("london " + i.ToString(), London));
        }
        NewCastel.AddChild(new Category("NewCastle 1", NewCastel));
        NewCastel.AddChild(new Category("NewCastle 2", NewCastel));
        NewCastel.AddChild(new Category("NewCastle 3", NewCastel));
        data.Add(UK);
        Category USA = new Category("USA", null);
        USA.AddChild(new Category("NewYork", USA));
        USA.AddChild(new Category("California", USA));
        data.Add(USA);
        Category China = new Category("China", null);
        China.AddChild(new Category("one", China));
        China.AddChild(new Category("two", China));
        China.AddChild(new Category("three", China));
        data.Add(China);
        return data;
    }

Behind a button i set your data to RadTreeListView

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        radTreeListView.ItemsSource = GetCategoryData();
    }

Now it's time to respond to your request! checking parents:

 private void radTreeListView_SelectionChanging(object sender, Telerik.Windows.Controls.SelectionChangingEventArgs e)
    {
        if (e.AddedItems.Count>0)
        {
            if (radTreeListView.SelectedItems.Count >= 5)
            {
                e.Cancel = true;
            }
            if (radTreeListView.SelectedItems.Count>=1) // a node has been selected before
            {
                //
                Category PreviousSelectedItem = (Category)radTreeListView.SelectedItems[0];
                Category ItemWhichisSelectedNow = (Category)e.AddedItems[0];
                if (PreviousSelectedItem.Parent != ItemWhichisSelectedNow.Parent)
                {
                    e.Cancel = true;
                }
            }
        }
    }

Upvotes: 2

Related Questions