pre-selection / checked item on load

10 posts, 0 answers
  1. Alan
    Alan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 09 Oct 2018 Link to this post

    Hi, 

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

    Thanks,

    Alan

  2. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 09 Oct 2018 Link to this post

    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
  3. Alan
    Alan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 09 Oct 2018 in reply to Lance | Manager Technical Support Link to this post

    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

     

     

  4. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 10 Oct 2018 Link to this post

    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
  5. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 10 Oct 2018 Link to this post

    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
  6. Alan
    Alan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 16 Oct 2018 in reply to Lance | Manager Technical Support Link to this post

    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

     

     

     

  7. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 19 Oct 2018 Link to this post

    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
  8. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 23 Oct 2018 Link to this post

    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
  9. Alan
    Alan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 24 Oct 2018 in reply to Stefan Nenchev Link to this post

    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

  10. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 25 Oct 2018 Link to this post

    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
Back to Top