我遇到以下我不理解的行为.我有正确显示在TreeView中的数据,如下所示.
Sport > BaseBall > Apparel > Equiptment Glove Bat > Football > Helmet > Soccer
但是,当我单击任何节点时,基础节点数据就是它的第一个子数据.
Node Clicked Actual Data ------------------------------------------- Sport Baseball Baseball Apparel Football Helmet Bat null
我已经在网上和书籍中查看了许多示例,但是我无法发现问题.而且我敢肯定这很简单.
编辑我已经目视检查了调试器中的每个节点,并使用了C#2010中Mathew MacDonald’s 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; }
}
这是我机器上的输出…
解决方法:
尝试切换
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设置中被继承,因此不妨使用它来节省多余的工作.