title: AntBlazor tree with checkboxes. How to check some tree items with code, when replacing the tree data
body: <p>I'm searching for a way how to set the check boxes in an AntBlazor Tree component by code for example on button press. Not at startup. In the end I would like to replace the tree data with a filtered set of tree data, and keep the check boxes checked that were checked before. In my attempts the check boxes are cleared when a new DataSource is set, and I found no way to re-set them again. I'm using AntBlazor 0.12.0.1. which is the latest version at this time. I used a simple test implementation : Checkable=&quot;true&quot;, @bind-CheckedKeys=&quot;CheckedKeys&quot; , DefaultCheckedKeys=&quot;DefaultCheckedKeys&quot;, DataSource=&quot;People&quot; (where people is List&lt; Person &gt; and Person has Children of List&lt; Person &gt; ) and no way to get to the checkbox item in the code using : @ref=&quot;PeopleTree&quot;. I also found no example code on how to do this. Any suggestions on how to approach this would be welcome.</p>
  <p>(2 days later)
  Finally  I found a way to get to the checked property in code : (all objects in this piece of code where found and not null )</p>
  <pre><code>public void ReplaceTree()
  {
      var x = CheckedKeys.ToList();
      CheckedKeys = null;
      People = CreateTree(); // Resets the tree data to loaded data, all checkboxes are cleared
      CheckedKeys = x.ToArray();
      resetCheckedItems(CheckedKeys);
      StateHasChanged();
  }
  
  
  private void resetCheckedItems(string[] checkedKeys)
  {
      foreach (string key in checkedKeys)
      {
          int id = key.ToInt();
          var treeNode = PeopleTree.FindFirstOrDefaultNode((node) =&gt; node.DataItem.Id == id, true);
          treeNode.SetChecked(true);
      }
  }
  </code></pre>
  <p>But even this does not seem to set the checks visually on the screen. All check boxes stay unchecked.</p>
  <p>The code is working and can be used to set the checkboxes, as long as the data source is not replaced. The replaced tree data is the same and all keys which are Id's are also the same, and even the treeNode is found when I use &quot;FindFirstOrDefaultNode&quot;, but the checkbox is not set now, when the data source has been replaced.</p>
  <p>(8 days later) I have found a work around. It involves keeping the dataSource. I have found duplicate entries in the tree, what makes that the nodes need to be removed further like this :</p>
  <pre><code>var treeNode = PeopleTree.FindFirstOrDefaultNode((node) =&gt; node.DataItem.Id == id, true);
  if (treeNode != null)
  {
      if (!removedTreeNodes.Contains(treeNode)) removedTreeNodes.Add(treeNode);
      treeNode.RemoveNode();
  }
  </code></pre>
  <p>When I then add the node again, it is unique and also the set checked will work if it will find the node again in the data.</p>
  <p>The problem is that it will not find the node in the same piece of code that will add the data. It first requires the code to complete, and a &quot;StateHasChanged&quot; to have ran in order for the component to update it's node structure based on the data.</p>
  <p>In order to set them checked in the same piece of code I created a function that runs asynchronously while the main code continues.</p>
  <p>The function waits 1ms so that the main code has finished, and the StateHasChanged has updated the Tree component. And then sets the checkbox to the state that was saved. :</p>
  <pre><code>public async Task SetChecked(TreeNode&lt;Person&gt; treenode)
  {
      await Task.Run(() =&gt; { Task.Delay(1).Wait(); });
  
      var treeNode = PeopleTree.FindFirstOrDefaultNode((node) =&gt; node.DataItem.Id == treenode.DataItem.Id, true);
      if (treeNode != null)
      {
          treeNode.SetChecked(treenode.Checked);
      }
  }
  </code></pre>