2016-04-08 40 views
0

我希望在每個節點的循環中能夠在節點訪問時以及在其離開將背景設置爲透明時再改變背景。在樹狀視圖中嵌套節點的wpf訪問堆棧面板

我的問題:我不知道如何訪問堆棧面板從後面的代碼更改BackGround。 我希望得到任何幫助

這裏是我的代碼: TreeView控件

<TreeView Grid.Column="1" Grid.Row="0" ItemsSource="{Binding ListOfNodes}" 
                Background="Linen" Margin="0,0,0,-0"> 
    <TreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsExpanded" Value="True"/> 
     </Style> 
    </TreeView.ItemContainerStyle> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate x:Name="HDT_node" DataType="Node" ItemsSource="{Binding Children}"> 
      <StackPanel Orientation="Horizontal" 
            MouseLeftButtonDown="btnTreeItemStartPlay" 
            Background="Transparent"> 
       <Image Source="{Binding Path=image.Source}" Width="30" Height="30" 
           HorizontalAlignment="Left" 
            MouseEnter="ZoomStart" MouseLeave="ZoomStop" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

等級節點:

public class Node 
{ 
    public Image image { get; set; } 

    public List<Node> Children { get; set; } 

    public Node() 
    { 
    } 

    public Node(Image imageIn, int orderIndexIn) 
    { 
     image = imageIn; 
     Children = new List<Node>(); 
    } 
} 
+0

與此類似:http://stackoverflow.com/questions/34117944/listbox-items-return-string-when-datatemplate-is-button –

回答

1

還有就是內DataTemplate訪問StackPanel沒有直接的方法。但是,仍然可以使用VisualTreeHelper在運行時遍歷可視樹,並執行任何您想要的操作。

在此之前,請使用WPF Visual Tree Visualizer來熟悉您擁有的Visual Tree。之後從TreeView開始,並重復兒童以獲得想要的孩子。根據您的模板,可視化樹會有所不同。

+0

非常感謝。它有幫助。 – Yefim

+0

@Yefim如果那是你正在尋找的東西,別忘了接受它 –