2012-03-27 74 views
1

我想實現拖放進行結合,以自定義元素TreeView控件相關的underyling元素:如何找到一個TreeViewItem

<TreeView ItemsSource="{Binding FirstGeneration}"> 
    <TreeView.Resources> 
    <HierarchicalDataTemplate DataType="{x:Type NavigationTreeElement}" 
           ItemsSource="{Binding Children}"> 
     <StackPanel Orientation="Horizontal" Margin="1"> 
      <Image Source="open-16x16.png" /> 
      <TextBlock Text="{Binding Path=DisplayName}" /> 
     </StackPanel> 
    </HierarchicalDataTemplate> 
    </TreeView.Resources> 
</TreeView> 

與基礎類:

public class NavigationElement 
{ 
    public string DisplayName { get; set; } 
    public ObservableCollection<NavigationElement> Children { get; set; } 
} 

我在將呈現的TreeViewItem與底層的NavigationElement關聯時遇到問題。如果我發現一個TreeViewItem鼠標已經結束,因爲這樣的:

private static TreeViewItem GetNearestContainer(UIElement element) 
    { 
     // Walk up the element tree to the nearest tree view item. 
     var container = element as TreeViewItem; 
     while ((container == null) && (element != null)) 
     { 
      element = VisualTreeHelper.GetParent(element) as UIElement; 
      container = element as TreeViewItem; 
     } 
     return container; 
    } 

如何是樹型視圖與下面NavigationElement關聯?

回答

1

我認爲這是container.Header,看起來很奇怪。

相關問題