好吧,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;
}
}
}
}