C# TreeView未存储预期状态

C# TreeView未存储预期状态,c#,wpf,treeview,wpf-controls,C#,Wpf,Treeview,Wpf Controls,我遇到以下我不理解的行为。我的数据正确显示在树视图中,如下所示 Sport > BaseBall > Apparel > Equiptment Glove Bat > Football > Helmet > Soccer 这是我机器上的输出 尝试切换 OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)((TreeViewItem)it).Items.CurrentItem);

我遇到以下我不理解的行为。我的数据正确显示在
树视图中,如下所示

Sport > BaseBall > Apparel > Equiptment Glove Bat > Football > Helmet > Soccer 这是我机器上的输出

尝试切换

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

在while循环中,您需要的是
treevieItem.DataContext
,而不是指向其子项的
Items.CurrentItem
(在首先展开之前,只有一个存在)。这就是为什么你看到的是副总裁而不是首席执行官

我会首先将整个函数切换为如下内容:

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);
}

然后在可视化树中迭代以获得
treevieItem
DataContext
在xaml设置中被继承,因此不妨使用它来保存冗余工作。

如何验证实际数据与单击的树节点不同?我已在调试器中目视检查了每个节点,以及使用Mathew MacDonald在C#2010中的Pro WPF中的一个方便的小代码片段,该代码片段实际上显示了树视图中每个控件的类型和值。如果显示的xaml已完成,则问题在于您没有在
树视图上设置
HierarchycalDataTemplate
,您正在将模板添加到
TreeView.Items
集合中。我很惊讶它没有抛出一个关于您设置
ItemsSource
和手动添加项目的异常。相反,模板应该位于我选中的
UserControl
@abehidebrecht的
参考资料中,您是正确的。我不小心漏掉了资源部分。我修改了XAML以反映问题。@JohnKraft好的,我不确定我能根据这里的信息解决问题,但我可以推荐一些调试WPF程序的工具。你试过Snoop()吗?虽然不是免费的,鼹鼠也有一些很大的支持。我知道这一定很简单。我假设DataContext属性将返回树绑定到的DataContext(通常是一个坏主意),所以我从未检查过它。谢谢你的帮助!
<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; }
}
OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)((TreeViewItem)it).Items.CurrentItem);
OrgChartNodeViewModel vm = ((OrgChartNodeViewModel)viewItem.DataContext);
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);
}