locked
Access controle in HierarchicalDataTemplate RRS feed

  • Question

  • In my project I create HierarchicalDataTemplate for tree that Include textbox and button

    the button is not visible. when MouseMove on node I want to set the button visibility to true on the node .

    My problem is  that i has no access to  HierarchicalDataTemplate items on code behind

    here is my Code  :

    1. HierarchicalDataTemplate :

    <UserControl.Resources> 
         
    <HierarchicalDataTemplate x:Key="TreeSubjects" ItemsSource="{Binding Path=Children}"> 
           
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
               
    <CheckBox x:Name="chbChecked" /> 
               
    <TextBlock x:Name="txbSubjectName" 
               
    Text="{Binding Path=SubjectName}" Margin="2,0,2,0"  VerticalAlignment="Center" /> 
               
    <Button x:Name="btnProperties" Content="Properties"  Visibility="Collapsed"/> 
           
    </StackPanel>                                                      
       
    </HierarchicalDataTemplate > 
    </UserControl.Resources>

     

    2. Tree :

    <sdk:TreeView  x:Name="trTree" ItemsSource="{Binding TreeSubjects}" 
                       
    ItemTemplate="{StaticResource TreeSubjects}"        
                       
    ItemContainerStyle="{StaticResource SubjectNodeStyle}"  MouseMove="trTree_MouseMove" 
                               
    /> 

    3. MoseMove :

    private void trTree_MouseMove(object sender, MouseEventArgs e )

    {

    ?????????

    }

     

    Sunday, May 15, 2011 3:49 AM

Answers

  • Handle the mouse move event on the stack panel inside the hierarchicalDataTemplate, then retrieve the button in the stack panel (Give a name to the button).

    private void hieStackPanel_MouseMove(object sender, MouseEventArgs)
    {
      StackPanel stackPanel = sender as StackPanel;
      Button myButton = stackPanel.FindName("myButtonName") as Button;
       if (myButton != null)
       {
        myButton.Visibility = Visibility.Visible;
       }
    }
    




     

    Sunday, May 15, 2011 6:38 AM

All replies

  • Hi,

    A suggestion:

    Use VisualTreeHelper to find that control.

    Sunday, May 15, 2011 4:20 AM
  • can you post sample how to do this for the button in the mousemove node only
    (not all buttons in the treeview )

    Sunday, May 15, 2011 5:31 AM
  • Handle the mouse move event on the stack panel inside the hierarchicalDataTemplate, then retrieve the button in the stack panel (Give a name to the button).

    private void hieStackPanel_MouseMove(object sender, MouseEventArgs)
    {
      StackPanel stackPanel = sender as StackPanel;
      Button myButton = stackPanel.FindName("myButtonName") as Button;
       if (myButton != null)
       {
        myButton.Visibility = Visibility.Visible;
       }
    }
    




     

    Sunday, May 15, 2011 6:38 AM
  • Hi,

    How's your issue going on?

    Have you try Algol79's suggestion?

     

    Regards

    Wednesday, May 18, 2011 7:04 AM