3 Answers, 1 is accepted
0
Hello Daniel,
The Telerik MAUI TreeView has a CheckedItems collection https://docs.telerik.com/devtools/maui/controls/treeview/checkboxes#checked-items-collection . The collection has getter and setter, so you can easily add, remove items to achieve the desired result.
I hope this will be of help.
Regards,
Didi
Progress Telerik
A brand new .NET MAUI course was just added to the Virtual Classroom. The training course is developed to help you get started with the Telerik UI for .NET MAUI components and features. It aims to put you in the shoes of an engineer who adds new features to an existing application. You can check it out at https://learn.telerik.com
0
Daniel
Top achievements
Rank 1
Iron
answered on 11 Sep 2023, 04:24 PM
Hi Didi, I was trying to update the collection by adding or removing items from the CheckedItems property but unfortunately, the UI didn't change.
I have the same scenario described in this question TreeControl checked.
How would be possible to iterate through the nodes on the treeView to set it as checked?
I have the same scenario described in this question TreeControl checked.
How would be possible to iterate through the nodes on the treeView to set it as checked?
0
Hello Daniel,
Regarding to this question -> How would be possible to iterate through the nodes on the TreeView to set it as checked? How to iterate through TreeView is a custom logic implementation. You have the items collection and checked items collection. how to add elements from one collection to another and iterate through thems is a development implementation.
Here is an example:
<Grid RowDefinitions="Auto,*">
<VerticalStackLayout >
<Button Clicked="Button_Clicked" Text="Add Item"/>
<Button Clicked="Button_Clicked_1" Text="Remove item"/>
</VerticalStackLayout>
<telerik:RadTreeView x:Name="treeView" Grid.Row="1"
AutomationId="treeView" CheckBoxMode="Recursive"
CheckedItems="{Binding CheckedItems}"
ItemsSource="{Binding Items}">
<telerik:TreeViewDescriptor DisplayMemberPath="Name"
ItemsSourcePath="Children"
TargetType="{x:Type local:Item}" />
</telerik:RadTreeView>
</Grid>
private void Button_Clicked(object sender, EventArgs e)
{
this.vm.CheckedItems.Add(this.vm.Items[0]);
foreach (var node in this.vm.Items)
{
this.vm.CheckedItems.Add(node);
foreach (var cityNode in node.Children)
{
this.vm.CheckedItems.Add(cityNode);
}
}
}
private void Button_Clicked_1(object sender, EventArgs e)
{
if (this.vm.CheckedItems.Count > 0)
{
this.vm.CheckedItems.Remove(this.vm.Items[0]);
}
}
public class ViewModel : NotifyPropertyChangedBase
{
public ViewModel()
{
Items = new ObservableCollection<Item>();
CheckedItems = new ObservableCollection<Item>();
Items.Add(new Item("My Projects")
{
Children = new List<Item>()
{
new Item("Using latest Telerik .NET MAUI controls")
{
Children = new ObservableCollection<Item>()
{
new Item("TreeView"),
new Item("Calendar"),
new Item("RichTextEditor"),
new Item("PdfViewer"),
new Item("SlideView"),
new Item("Chat"),
}
},
new Item("Blank project")
}
});
Items.Add(new Item("Shared Documents")
{
Children = new List<Item>()
{
new Item("Reports")
{
Children = new List<Item>()
{
new Item("October"),
new Item("November"),
new Item("December")
}
}
}
});
}
public ObservableCollection<Item> Items { get; set; }
private ObservableCollection<Item> checkedItems;
public ObservableCollection<Item> CheckedItems
{
get => this.checkedItems;
set
{
if (this.checkedItems != value)
{
this.checkedItems = value;
this.OnPropertyChanged();
}
}
}
}
public class Item
{
public Item(string name)
{
this.Name = name;
this.Children = new ObservableCollection<Item>();
}
public string Name { get; set; }
public IList<Item> Children { get; set; }
}