2012-08-22 46 views
0

我試圖讓TreeView控件在我的應用程序通過設置其ItemsSourceDataContext性質正確綁定到對象樹一個TreeView。該樹按預期形象化,但TreeViewItem的數據上下文似乎存在不正確的值。數據綁定使用HierarchicalDataTemplate

例如,我有一棵樹,看起來像這樣:

[-] Header 
    [-] Contents 
     [+] Item1 
     [+] Item2 
     properties 
    [+] Dictionary 
[-] MetaDictionary 
    [+] TypeDef1 
    [+] TypeDef2 
properties 

的項目被綁定到對象的Data.Name值。但是,如果我點擊是Header孩子的任何項目,並在事件處理程序檢查它,它DataContext.Data.NameHeader(經過適當的鑄件)。 MetaDictionary及其子女也會發生同樣的情況。

這是我班的一個縮短的版本:

public class CFItemTreeNode 
    { 
     private CFItem data; 
     public CFItem Data 
     { 
      get { return data; } 
      set { data = value; } 
     } 
     private ObservableCollection<CFItemTreeNode> children; 
     public ObservableCollection<CFItemTreeNode> Children 
     { 
      //set & get as above 
     } 
     private CFItemTreeNode parent; 
     public CFItemTreeNode Parent 
     { 
      //set & get as above 
     } 
    } 

這是我的XAML。我一直很精練,淨幾天,我已經納入位和各種教程和問題的塊放入這個怪人礦井。我相信這是分層模板的問題,但是就我所知。

<Window x:Class="SSLowLevelBrowser.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SSLowLevelBrowser" 
     Title="MainWindow" 
     Height="600" 
     Width="800" 
     MinHeight="100" 
     MinWidth="200" 
     Closing="MainWindowClosing"> 

    <Window.Resources> 
     <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}"> 
      <!-- A margin of 0 px left and 2 px top --> 
      <Setter Property="Margin" Value="0 2" /> 
      <Setter Property="AllowDrop" Value="true" /> 
      <EventSetter Event="TreeViewItem.PreviewMouseLeftButtonDown" Handler="TVI_PreviewMouseLeftButtonDown" /> 
      <EventSetter Event="TreeViewItem.PreviewMouseMove" Handler="TVI_PreviewMouseMove" /> 
      <EventSetter Event="TreeViewItem.PreviewDrop" Handler="TVI_PreviewDrop" /> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="25" /> 
      <RowDefinition Height="575*" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="390*" /> 
      <ColumnDefinition Width="390*" /> 
     </Grid.ColumnDefinitions> 

     <ToolBar Name="menuBar" Grid.ColumnSpan="2" ToolBarTray.IsLocked="True"> 
      <Button Name="buttonOpen" Click="OpenFile">Open file</Button> 
     </ToolBar> 
     <TreeView Grid.Row="1" 
        Grid.Column="0" 
        Name="treeView" 
        ItemContainerStyle="{StaticResource TreeViewItemStyle}" 
        ItemsSource="{Binding}"> 
      <TreeView.Resources> 
       <HierarchicalDataTemplate DataType="{x:Type local:CFItemTreeNode}" ItemsSource="{Binding Children}"> 
        <Grid> 
         <TextBlock Text="{Binding Path=Data.Name}" 
            MouseLeftButtonDown="TBlock_PreviewMouseLeftButtonDown"/> 
         <TextBox Text="{Binding Path=Data.Name, Mode=TwoWay}" 
           Visibility="Collapsed" 
           LostFocus="TBox_LostFocus"/> 
        </Grid> 
       </HierarchicalDataTemplate> 
      </TreeView.Resources> 
     </TreeView> 
     <TextBox Grid.Row="1" Grid.Column="1" Name="textOutput" /> 
    </Grid> 
</Window> 

我在做什麼錯?

更新1這裏是我的事件處理程序:

private void TVI_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs args) 
{ 
    dragStartPosition = args.GetPosition(null); 
    dragSource = args.OriginalSource; 
} 
private void TVI_PreviewMouseMove(object sender, MouseEventArgs args) 
{ 
    Point currentPosition = args.GetPosition(null); 
    // If there is actual movement and a drag is starting 
    if (dragInProgress == false && 
     dragStartPosition.X != -1 && 
     args.LeftButton == MouseButtonState.Pressed && 
     Math.Pow(currentPosition.X - dragStartPosition.X, 2) + 
     Math.Pow(currentPosition.Y - dragStartPosition.Y, 2) > 25) 
    { 
     dragInProgress = true; 
     DragDropEffects de = DragDrop.DoDragDrop(
      (TreeViewItem)sender, 
      new DataObject(typeof(FrameworkElement), dragSource), 
      DragDropEffects.Move); 
    } 
} 
private void TVI_PreviewDrop(object sender, DragEventArgs args) 
{ 
    if (dragInProgress && args.Data.GetDataPresent(typeof(FrameworkElement))) 
    { 
     CFItemTreeNode dragSource = 
      ((CFItemTreeNode)((FrameworkElement)args.Data.GetData(typeof(FrameworkElement))).DataContext); 
     CFItemTreeNode dropTarget = 
      ((CFItemTreeNode)((FrameworkElement)args.OriginalSource).DataContext); 
     CFItemTreeNode sourceParent = dragSource.Parent; 
     CFItemTreeNode targetParent = dropTarget.Parent; 
     if (sourceParent != targetParent) 
     { 
      MessageBox.Show("Can only swap siblings."); 
      dragInProgress = false; 
      return; 
     } 
     int sourceIndex = sourceParent.Children.IndexOf(dragSource); 
     int targetIndex = sourceParent.Children.IndexOf(dropTarget); 

     if (sourceIndex != targetIndex) 
     { 
      if (sourceIndex < targetIndex) 
      { 
       sourceParent.Children.RemoveAt(targetIndex); 
       sourceParent.Children.RemoveAt(sourceIndex); 
       sourceParent.Children.Insert(sourceIndex, dropTarget); 
       sourceParent.Children.Insert(targetIndex, dragSource); 
      } 
      else 
      { 
       sourceParent.Children.RemoveAt(sourceIndex); 
       sourceParent.Children.RemoveAt(targetIndex); 
       sourceParent.Children.Insert(targetIndex, dragSource); 
       sourceParent.Children.Insert(sourceIndex, dropTarget); 
      } 
     } 
     dragSource = null; 
     dragInProgress = false; 
     // Reset start position to invalid 
     dragStartPosition = new Point(-1, -1); 
    } 
} 
+0

向我們展示您的事件處理程序代碼。 – NestorArturo

+0

用事件處理程序更新了問題。 – mcmlxxxvi

回答

0

添加RelativeSource={RelativeSource AncestorType=local:MainWindow}到你綁定。

+0

哦,我的。我剛剛意識到問題的年代。 – 2014-11-14 12:23:25

+0

沒問題。不幸的是,我無法再訪問源代碼來驗證解決方案。 – mcmlxxxvi

相關問題