0

如何使telerik radTreeListView 只允许选择相同rootItem/ParentItem 下的项目?

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

从上面的 radTreeListView 示例菜单中,应该只允许用户选择

英国、美国或中国。 OR London, Newcastle, Liverpool, OR Newcastle 1, Newcastle 2, Newcastle 3 BUT NOT Newcastle 1, London 1, UK ALSO NOT UK, Newcastle, Newcastle 1。

用户应该只选择同一层次结构中的事物/项目。基本上,他们不应该选择父母和孩子,他们要么从父母或孩子或子孩子中选择。

这是我正在使用的控件:http ://www.telerik.com/help/silverlight/radtreelistview-overview.html

4

1 回答 1

2

好吧,manually如果你在类中填充数据并为每个数据设置一个父节点,你应该这样做,你可以检查谁是选定的节点父节点!

那么现在如何填充数据呢?

试试这个更常见的方法,Telerik 的网站上也提供了一个示例!

我尝试您的示例数据,如下所示,并享受学习新事物的乐趣。

创建一个包含父类的类

看到这个类:

 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();
            }
        }
    }

现在我尝试生成您的样本数据“UK-USA-China”

这是生成示例数据的代码

  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;
    }

在按钮后面,我将您的数据设置为 RadTreeListView

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

现在是时候响应您的请求了! 检查父母:

 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;
                }
            }
        }
    }
于 2013-11-07T04:25:18.007 回答