3

我遇到了以下我不明白的行为。我有如下正确显示的数据。TreeView

运动
  > 棒球
    > 服装
    > 设备
        手套
        蝙蝠
  > 足球
    > 头盔
  > 足球

但是,当我单击任何节点时,底层节点数据是它的第一个子节点数据。

    节点点击的实际数据
------------------------------------------
    运动棒球
    棒球服装
    足球头盔
    蝙蝠空

我在网上和书本上看过很多例子,但我找不到问题;我敢肯定这很简单。

编辑我已经在调试器中直观地检查了每个节点,并使用了 C# 2010 中 Mathew MacDonald 的 Pro WPF 中的一个方便的小代码片段,它实际上显示了 TreeView 中每个控件的类型和值。

编辑我已将初始代码替换为重现该问题的实际完整应用程序。这是我能想到的最简单的例子。

代码(删除了不相关的部分):

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:this="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <this:MainWindowViewModel x:Key="ViewModel:MainWindow"></this:MainWindowViewModel>
    </Application.Resources>
</Application>

-

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{StaticResource ViewModel:MainWindow}">
    <TreeView x:Name="theTree" ItemsSource="{Binding Path=OrgChart}"
              MouseUp="theTree_MouseUp">
        <TreeView.Resources>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Subordinates}" DataType="{x:Type this:OrgChartNodeViewModel}">
                <TextBlock Text="{Binding Path=Position.Title}"></TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Window>

-

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void theTree_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Stack<DependencyObject> stack = new Stack<DependencyObject>();
        var it = e.OriginalSource as DependencyObject;
        while (it != null)
        {
            it = VisualTreeHelper.GetParent(it);
            stack.Push(it);
        }

        int level = 0;
        while (stack.Any())
        {
            Debug.Write("".PadLeft(level++));
            it = stack.Pop();
            if (it is TreeViewItem)
            {
                var item = it as TreeViewItem;
                OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)((TreeViewItem)it).Items.CurrentItem);
                if (vm != null)
                    Debug.WriteLine(vm.Position.Title);
            }
            else
            {
                Debug.WriteLine(it);
            }
        }
    }
}

-

public class MainWindowViewModel
{
    public List<OrgChartNodeViewModel> OrgChart { get; set; }

    public MainWindowViewModel()
    {
        Position ceo = new Position { Title = "CEO" };
        Position vp = new Position { Title = "VP" };
        Position boss = new Position { Title = "Boss" };
        Position worker = new Position { Title = "Worker" };

        OrgChartNodeViewModel root;
        OrgChartNodeViewModel node = new OrgChartNodeViewModel { Position = ceo };
        OrgChartNodeViewModel child = new OrgChartNodeViewModel { Position = vp };
        root = node;
        node.Subordinates.Add(child);
        node = child;
        child = new OrgChartNodeViewModel { Position = boss };
        node.Subordinates.Add(child);
        node = child;
        child = new OrgChartNodeViewModel { Position = worker };
        node.Subordinates.Add(child);

        OrgChart = new List<OrgChartNodeViewModel> { root };
    }
}

-

public class OrgChartNodeViewModel
{
    public Position Position { get; set; }
    public List<OrgChartNodeViewModel> Subordinates { get; set; }

    public OrgChartNodeViewModel()
    {
        Subordinates = new List<OrgChartNodeViewModel>();
    }
}

-

public class Position
{
    public string Title { get; set; }
}

这是我机器上的输出...

在此处输入图像描述

4

1 回答 1

3

尝试切换

OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)((TreeViewItem)it).Items.CurrentItem);

OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)viewItem.DataContext);

在您的 while 循环中,您想要TreeViewItem.DataContext而不是Items.CurrentItem指向它的孩子(并且只有一个存在,直到首先展开)。因此,为什么您看到的是 VP 而不是 CEO。

我会将整个功能切换为以下内容:

private void theTree_MouseUp(object sender, MouseButtonEventArgs e) {
  var clickedItem = e.OriginalSource as FrameworkElement;
  if (clickedItem == null)
    return;
  var chartNode = clickedItem.DataContext as OrgChartNodeViewModel;
  if (chartNode != null)
    Debug.WriteLine(chartNode.Position.Title);
}

而不是遍历 Visual-Tree 以获得TreeViewItem. DataContext在您的 xaml 设置中继承,因此不妨使用它来节省做多余的工作。

于 2013-06-28T03:33:05.340 回答