9 Answers, 1 is accepted
You can do this by programmatically adding the items you want checked to the CheckedItems collection. See the following documentation for more information: CheckBox Elements (find the code for the examples in that article here).
.
For example:
myTreeView.ItemsSource = myItems;
var firstItem = myItems.FirstOrDefault();
if
(firstItem !=
null
)
{
myTreeView.CheckedItems.Add(firstItem);
}
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Hi Lance,
Thanks for the reply. I am using R3 2018, the CheckedItems return type of TreeViewItemsCollection does not have the Add method
Check the API does not find the Add method as well.
https://docs.telerik.com/devtools/xamarin/apireference/html/24513d45-4489-43d0-2c4c-05e573680444.htm#!P
please advise.
Thanks,
Alan
My apologies, that approach isn't available yet (we're working on it now). In the meantime, you can still accomplish what you want by using the TreeView's CheckItem and UncheckItem methods:
As an example, let's say the TreeView's ItemsSource was ObservableCollection<HomeMenuItem>, then you could do this:
private void MyTreeView_OnNativeControlLoaded(object sender, EventArgs e)
{
var treeView = sender as RadTreeView;
var allItems = treeView.ItemsSource as ObservableCollection<
HomeMenuItem
>;
var firstItem = allItems.FirstOrDefault();
treeView.CheckItem(firstItem);
}
I'm going to update the documentation as soon as possible to explain these methods.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
I wanted to quickly follow up with you to share a demo to see this at runtime. Run the attached demo, you'll see the first item change its checked state every time you click the button.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Hi Lance,
Thanks for your demo. I have no issue with check or uncheck tree view item thru user interaction. What i would like to achieve is to pre-check item when the tree view is first time loading.
Our business use case - we have a product catalog and admin is able to assign/re-assign products to customer. After a customer has been assigned to products, subsequently when admin need to re-assign product, the product tree will be loaded with product checked which indicates a product is currently assigned.
I have tried the MyTreeView_OnNativeControlLoaded event but i getting NULL for var allItems = treeView.ItemsSource as ObservableCollection<HomeMenuItem>;
Any other solution you can advise will be appreciated.
Regards,
Alan
The approach for pre-checking the items is to call the methods when the control is loaded as Lance has shown in the example. Indeed, I was able to reproduce the NullReferenceException. I will need some more time to investigate the behavior and suggest a possible workaround or another approach which you can use. I will update the thread as soon as we have some information.
Have a great weekend.
Regards,
Stefan Nenchev
Progress Telerik
Thank you for your patience.
I have logged the behavior as a bug at our end. You can track its progress on the following link - TreeView: NullReferenceException when calling CheckItem in NativeControlLoaded. Please subscribe to the public item so that you are automatically notified when there is an update. I have also added some points to your account for reporting the behavior.
In the meantime, the only viable workaround we could come up with at this point is to execute the checking of the items with some delay. For example:
private
void
MyTreeView_NativeControlLoaded(
object
sender, EventArgs e)
{
Device.StartTimer(TimeSpan.FromSeconds(0.3), () =>
{
var source = MyTreeView.ItemsSource
as
ObservableCollection<MenuItem>;
var firstItem = source.FirstOrDefault();
MyTreeView.CheckItem(firstItem);
return
true
;
});
}
Though this is not ideal as the time will vary on devices, it should serve as a temporary workaround until we come up with a fix.
Have a great rest of the week.
Regards,
Stefan Nenchev
Progress Telerik
Hi Stefan,
Thanks for the update. I tried the interim solution, items are checked on load, however after unchecked one or more items, it was automatically checked back. Instead of using Device.StartTimer, I replaced it with await Task.Delay(1000); the auto checking back behaviour disappeared. I will use this as interim solution.
Thanks,
Alan
Great to here that you found a viable workaround and thanks for sharing it here in case other customers need it as well.
Regards,
Stefan Nenchev
Progress Telerik