This is a migrated thread and some comments may be shown as answers.

pre-selection / checked item on load

9 Answers 65 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 09 Oct 2018, 08:25 AM

Hi, 

How do i pre-select/check one more more items when first time loading the treeview? 

Thanks,

Alan

9 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 09 Oct 2018, 04:11 PM
Hi Alan,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Alan
Top achievements
Rank 1
answered on 10 Oct 2018, 02:24 AM

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

 

 

0
Lance | Manager Technical Support
Telerik team
answered on 10 Oct 2018, 02:39 PM
Hi 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lance | Manager Technical Support
Telerik team
answered on 10 Oct 2018, 03:28 PM
Hello Alan,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Alan
Top achievements
Rank 1
answered on 17 Oct 2018, 03:19 AM

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

 

 

 

0
Stefan Nenchev
Telerik team
answered on 19 Oct 2018, 02:35 PM
Hi, 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Stefan Nenchev
Telerik team
answered on 23 Oct 2018, 07:12 AM
Hi, Alan,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Alan
Top achievements
Rank 1
answered on 24 Oct 2018, 06:55 AM

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

0
Stefan Nenchev
Telerik team
answered on 25 Oct 2018, 07:41 AM
Hello, 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
TreeView
Asked by
Alan
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Alan
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or