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

Casting var into RadTreeViewItem

4 Answers 60 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Srikrishna
Top achievements
Rank 1
Srikrishna asked on 28 Jun 2012, 02:50 PM

Hi Telerik,

I am using RadTreeView in my application. I am getting the RadTreeView Items dynamically from the database in this way:
LoadOperation<tblRole> LoadRole = this.context.Load(this.context.GetRoleQuery());
TreeView_Role.ItemsSource = LoadRole.Entities;
LoadRole.Completed += new EventHandler(LoadRole_Completed);

 

When i try to iterate through the RadTreeViewItems and cast var into RadTreeViewItem as shown below, it is always showing null.
void LoadRole_Completed(object sender, EventArgs e)
      {
          foreach (object ob in TreeView_Role.Items)
          {
              RadTreeViewItem ritem = TreeView_Role.ContainerFromItemRecursive(ob);
          }  
      }

Please help me solve this Issue. Thanks in Advance.

4 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 28 Jun 2012, 03:01 PM
Hi Srikrishna,

This behavior is expected as the RadTreeView control hasn't generated its RadTreeViewItem containers at this point. Basically in order to get the containers of type RadTreeViewItem that wrap teh business colelction, yo uneed to make sure that they are already generated.

For example you can handle the RadTreeView.ItemContainerGenerator.StatusChanged event handler to make sure that the containers are generated before traversing the Items collection:
   TreeView_Role.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
...
        void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if ((sender as Telerik.Windows.Controls.ItemContainerGenerator).Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                //traverse the RadTreeView.Items collection
            }
        }

If you give us more details about your scenario and application, we will eb able to suggest more suitable approaches.

Greetings,
Tina Stancheva
the Telerik team

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

0
Srikrishna
Top achievements
Rank 1
answered on 29 Jun 2012, 04:56 AM

Thanks Tina for the reply. I am now able to access my TreeViewItems.

I have another question regarding telerik RadGridview. I have a gridviewdatacolumn which is binded to a checkbox as below:
<telerik:RadWindow.Resources>
       <DataTemplate x:Key="BindBUName">
           <StackPanel Orientation="Horizontal">
               <CheckBox Content="{Binding sgBusinessUnitName, Mode=TwoWay}"></CheckBox>
           </StackPanel>
       </DataTemplate>
</telerik:RadWindow.Resources>
<telerik:RadGridView x:Name="TelGV_BusinessUnit" SelectionMode="Multiple" AutoGenerateColumns="False" Margin="20,0,0,0" Height="477" Width="420">
                           <telerik:RadGridView.Columns>
                               <telerik:GridViewDataColumn x:Name="ColumnBUgroup" Header="Business Unit Group" CellTemplate="{StaticResource BindBUName}" CellTemplateSelector=""></telerik:GridViewDataColumn>
                           </telerik:RadGridView.Columns>
                       </telerik:RadGridView>

My requirement is when user checks the checkbox the row also should be selected so that i can access the selected items in code behind. How can i achieve this?

Thanks.

0
Lancelot
Top achievements
Rank 1
answered on 02 Jul 2012, 04:02 PM
Hi Srikrishna,

You can accomplish this through the SelectionChanged event. This documentation will show you the Selection events of the RadGridView. Notice in the top section it describes that the SelectionChanged event will select the entire row.

In the constructor of the page create the SelectionChanged event handler and then for the CheckBox create a click event. You'd want to do a simple 

if(myCheckBox == IsChecked)
{
     SelectionChanged();
}

before continuing. Then use the if statement to do what you want to the row's items, this is especially true if you are already using the SelectionChanged event for other methods. Just run what you want inside the if statement, like this:

private void SelectionChanged(object sender, SelectionChangeEventArgs e )
{
    if(myCheckBox == IsChecked)
{
      Run your checkbox specific code in here
}  
}


I hope this helps you 

Good Luck,
Lancelot
0
Srikrishna
Top achievements
Rank 1
answered on 04 Jul 2012, 02:01 PM

Hi Lancelot,

I tried by the way you suggested. But it didn't work. The checked or click event is not raised when the item is checked as iam dynamically binding the items into treeview.
Below is my code for RadGridView:
Am i doing something wrong here?
<telerik:RadWindow.Resources>
        <DataTemplate x:Key="BindBUName">
            <StackPanel Orientation="Horizontal">
                <CheckBox x:Name="cbBUname" Content="{Binding sgBusinessUnitName}"></CheckBox>
            </StackPanel>
        </DataTemplate>
 </telerik:RadWindow.Resources>
 
 <telerik:RadGridView x:Name="TelGV_BusinessUnit" SelectionMode="Multiple" AutoGenerateColumns="False" IsReadOnly="True" Margin="20,0,0,0" Height="457" Width="420">
                            <telerik:RadGridView.Columns>
                                <telerik:GridViewDataColumn x:Name="ColumnBUgroup" Header="Business Unit Group" CellTemplate="{StaticResource BindBUName}"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn x:Name="ColumnBU" Header="Business Unit" CellTemplate="{StaticResource BindBUName}"></telerik:GridViewDataColumn>
                            </telerik:RadGridView.Columns>
</telerik:RadGridView>
Tags
TreeView
Asked by
Srikrishna
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Srikrishna
Top achievements
Rank 1
Lancelot
Top achievements
Rank 1
Share this question
or