Identifying TreeViewItems when embedded UIElements are clicked

2 posts, 1 answers
  1. Marcus
    Marcus avatar
    3 posts
    Member since:
    Sep 2010

    Posted 07 Oct 2010 Link to this post


    I'm new to C# and Silverlight, so I might just be on the completely wrong way:

    I have a set of TreeViews of this kind:


    <telerikCore:HierarchicalDataTemplate x:Name="MainTemplate" ItemsSource="{Binding Children, Mode=TwoWay}" telerikCore:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}"
                    <StackPanel Orientation="Horizontal" Background="Transparent"
                        <StackPanel x:Name="ListPanel" Margin="30,3,30,3" Orientation="Horizontal" Visibility="{Binding IsRuleVisible, Converter={StaticResource VisibleConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center"
                            <TextBox x:Name="RuleAttributeInput" Text="{Binding RuleAttribute, Mode=TwoWay}" Width="200" MaxWidth="200" IsEnabled="False"></TextBox
                            <Button Content="V" x:Name="MetaDataWindow" Click="MetaDataWindow_Click"></Button
                            <ComboBox x:Name="OperatorComboBox" 
                                Width="100" Margin="5 0 0 0"
                                ItemsSource="{Binding NumOpList}" 
                                SelectedValue="{Binding RuleOperator, Mode=TwoWay}"
                            <telerikInput:RadMaskedTextBox x:Name="RuleValueInput"
                                Width="100" Margin="5 0 0 0" FontSize="10"
                                MaskType="None" Value="{Binding RuleValue, Mode=TwoWay}" /> 
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Visibility="{Binding IsOperatorVisible, Converter={StaticResource VisibleConverter}}"
                            <TextBlock Text="{Binding LogOperator, Mode=TwoWay, Converter={StaticResource LogOpConverter}}" Width="40" Margin="10" TextAlignment="Center"></TextBlock
    The Treeview itself:
    <telerik:RadTreeView x:Name="RuleTreeView" SelectionMode="Multiple"
                  ItemContainerStyle="{StaticResource TreeViewItemStyle}" ItemTemplate="{StaticResource MainTemplate}" 
                  IsExpandOnSingleClickEnabled="False" DragStarted="RadTreeView_DragStarted" HorizontalAlignment="Left" 
                  HorizontalContentAlignment="Left" IsDragDropEnabled="True" IsEditable="True" IsSingleExpandPath="False" IsExpandOnDblClickEnabled="False">

    When opening a window to get some detailed informations on some items I'm restricted on passing the value of the UI Elements, in this case I'm passing the first Textbox (RuleAttributeInput).
    private void MetaDataWindow_Click(object sender, RoutedEventArgs e)
        MyWindow newWindow = new MyWindow(((System.Windows.Controls.Panel)((System.Windows.FrameworkElement)sender).Parent).Children[0]);

    In order to have access to the TreeViewElement (and furthermore its attributes) the user is working in, I want to pass the TreeViewElement to the Window, but I'm not able to get it right. When using parent I get to the top tier stackpanel but always end up with a null element.

    Of course I could force users to first select the treeviewelement to get me the selectedItem, but maybe theres a way without needing to select any TreeviewElements (and just click on the Button).

    Any idea on how to identify a TreeviewElement when having only an embedded UIElement as Sender?

  2. Answer
    Petar Mladenov
    Petar Mladenov avatar
    3152 posts

    Posted 08 Oct 2010 Link to this post

    Hi Marcus,

    Please take a look at the following code demonstrating how to access the first parent TreeViewItem of the button and  its children items.

    private void MetaDataWindow_Click(object sender, RoutedEventArgs e)
        //accessing the first parent TreeViewItem
        StackPanel firstOuterStack = (StackPanel)((sender as FrameworkElement).Parent);
        StackPanel secondOuterStack = (StackPanel)(firstOuterStack.Parent);
        RadTreeViewItem tViewitem = (RadTreeViewItem)(secondOuterStack.Parent);
        //accessing the TreeViewItem children
        StackPanel firstChildStack = tViewitem.Items[0] as StackPanel;
        StackPanel firstGrandChildStack = firstChildStack.Children[0] as StackPanel;
        StackPanel secondGrandChildStack = firstChildStack.Children[1] as StackPanel;
        MessageBox.Show((secondOuterStack == firstChildStack).ToString() + "\n" +
                        (firstOuterStack == firstGrandChildStack).ToString());
        TextBox textBox = firstGrandChildStack.Children[0] as TextBox;
        string tBoxText = textBox.Text;
        ComboBox combo = firstGrandChildStack.Children[2] as ComboBox;
        string comboValue = combo.SelectedValue.ToString();
        RadMaskedTextBox maskeBox = firstGrandChildStack.Children[3] as RadMaskedTextBox;
        string maskedVal = maskeBox.Value.ToString();
        TextBlock tBlock = secondGrandChildStack.Children[0] as TextBlock;
        string blocktext = tBlock.Text;

    I hope this helps you and I would be glad if I can further assist you.

    Petar Mladenov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top