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

Identifying TreeViewItems when embedded UIElements are clicked

1 Answer 54 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Marcus
Top achievements
Rank 1
Marcus asked on 07 Oct 2010, 08:04 PM
Hi,

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"
    
                    <!--Rules-->
                    <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}"
                            Loaded="OperatorComboBox_Loaded"/> 
                        <telerikInput:RadMaskedTextBox x:Name="RuleValueInput"
                            Width="100" Margin="5 0 0 0" FontSize="10"
                            MaskType="None" Value="{Binding RuleValue, Mode=TwoWay}" /> 
                    </StackPanel
    
                    <!--Operators-->
                    <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
                    </StackPanel
    
                </StackPanel>
</telerikCore:HierarchicalDataTemplate>
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">
</telerik:RadTreeView>

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]);
    newWindow.Show();
}

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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 08 Oct 2010, 02:45 PM
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.

Regards,
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
Tags
TreeView
Asked by
Marcus
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or