Recheck the checkbox by programatically

4 posts, 0 answers
  1. Asif
    Asif avatar
    49 posts
    Member since:
    Jul 2010

    Posted 10 Nov 2011 Link to this post

    Hello Support
    I have used an RadTreeview
    <telerik:RadTreeView x:Name="radTreeView" Margin="8" IsTriStateMode="true" IsOptionElementsEnabled="True" ItemsOptionListType="CheckList" DataContext="{StaticResource dataSetProvider}" ItemsSource="{Binding Group}" ItemTemplate="{StaticResource MasterTemplate}">
    </telerik:RadTreeView>

    It looks like
    A
      A.1
      A.2
      A.3
    B
      B.1
      B.2

    Now while saving the information user has checked on checkboxes for example A.1 & A.2 only
    When he revisits the screen I need to show the checkboxes of A.1 & A.2 has checked. How to do this? Tried many of your forums
    all the references is pointing towards following line of code.
    for (int i = 0; i < rdGroups.radTreeView.Items.Count; i++)
     {
         RadTreeViewItem container = rdGroups.radTreeView.ItemContainerGenerator.ContainerFromIndex(i) as RadTreeViewItem;
     
         for (int x = 0; x < container.Items.Count; x++)
         {              
            if ((container.Items[x] as System.Data.DataRowView).Row.ItemArray[0].ToString() == strLocationIds[j])
            {
              RadTreeViewItem ch = rdGroups.radTreeView.ItemFromContainer(container.Items[x]) as RadTreeViewItem;
              ch.CheckState = System.Windows.Automation.ToggleState.On;
     
          //I am getting ch as null. hence it is giving error for object reference.
            }
                             
         }
    }

    Please suggest me where I am getting wrong.
    Thanks
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 14 Nov 2011 Link to this post

    Hello Asif,

     The ItemFromContainer() method must return the business object inside the RadTreeViewItem when the RadTreeView is bound to collection of objects/ ViewModels. On the other hand, if you need to access the container (the RadTreeViewItem) that hold the business object, you need to use ContainerFromItemRecursive(). However, please keep in mind that this is potentially expensive method that traverses the RadTreeView. Having this in mind, you could try to use ContainerFromItem() method but still this won't be elegant. We suggest you to use introduce boolean property in your ViewModels and bind the IsChecked property of the RadTreeViewItems to this property with Style Bindings. This way, when re-visiting the page, you won't need to find and check particular items, they will be bind to the models and the models won't be changed during page-reloading process. Please let us know if this helped you or not.


    All the best,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Asif
    Asif avatar
    49 posts
    Member since:
    Jul 2010

    Posted 16 Nov 2011 Link to this post

    I have binded my treeview from an DataSet which two DataTables and an relation between them
    I don't know to bind the isselected property in style binding.

     <Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadTreeViewItem}">
                <Setter Property="IsSelected" Value="{Binding Path=Selected, Mode=TwoWay}"/>
            </Style>
            <ObjectDataProvider x:Key="dataSetProvider" MethodName="CreateDataSet" ObjectType="{x:Type local:DataSetCreator}" />

            <DataTemplate x:Key="DetailTemplate">
                <TextBlock Text="{Binding LocationName}" />
            </DataTemplate>

            <HierarchicalDataTemplate x:Key="MasterTemplate" ItemsSource="{Binding Group2Location}"
                    ItemTemplate="{StaticResource DetailTemplate}">
                <TextBlock Text="{Binding GroupName}" FontWeight="Bold" />
            </HierarchicalDataTemplate>

    <telerik:RadTreeView x:Name="radTreeView" Margin="8" IsTriStateMode="true" IsOptionElementsEnabled="True" ItemsOptionListType="CheckList"
                     DataContext="{StaticResource dataSetProvider}"
                     ItemsSource="{Binding Group}"
                     ItemTemplate="{StaticResource MasterTemplate}"
                     ItemContainerStyle="{StaticResource ItemContainerStyle}">
            </telerik:RadTreeView>

    public class DataSetCreator
        {
            public static DataSet CreateDataSet()
            {
                DataSet ds = new DataSet();

                DataTable tbl = new DataTable("Group");
                tbl.Columns.Add("GroupId", typeof(int));
                tbl.Columns.Add("GroupName");

                tbl.Rows.Add(1, "G1");
                tbl.Rows.Add(2, "G2");

                ds.Tables.Add(tbl);

                tbl = new DataTable("Location");
                tbl.Columns.Add("LocationId", typeof(int));
                tbl.Columns.Add("GroupId", typeof(int));
                tbl.Columns.Add("LocationName");

                tbl.Rows.Add(1,1, "G1.1");
                tbl.Rows.Add(2,1, "G1.2");
                tbl.Rows.Add(3,2, "G2.1");
                tbl.Rows.Add(4,2, "G2.2");

                ds.Tables.Add(tbl);

                ds.Relations.Add("Group2Location", ds.Tables["Group"].Columns["GroupId"], ds.Tables["Location"].Columns["GroupId"]);

                return ds;
            }
        } 
    When I am looping through radtreeview.items I am getting DataRowView and not RadTreeviewItem. please suggest me.
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 21 Nov 2011 Link to this post

    Hello Asif,

     This is expected, when a RadTreeView is bound to collection the ItemsSource and the Items collections return the business collection that the tree is bound to. The best way to get the RadTreeViewItem that "wraps" a business item is to use the GetItemByPath() method. Please check out this help article showing how to use it. You may also find this demo useful.

    All the best,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top