我遇到以下我不明白的行爲。我的數據正確顯示在TreeView
如下。TreeView不存儲預期狀態
Sport > BaseBall > Apparel > Equiptment Glove Bat > Football > Helmet > Soccer
但是,當我點擊任何節點時,底層節點數據就是它的第一個子節點。
Node Clicked Actual Data ------------------------------------------- Sport Baseball Baseball Apparel Football Helmet Bat null
我看了很多網絡和書籍的例子,但是我找不到這個問題;我相信這很簡單。
編輯我已目視檢查,實際上顯示類型和在TreeView每個控制的值在C#2010在調試器的每個節點,以及從馬修麥克唐納的Pro WPF使用一個方便的小代碼段。
編輯我已經用實際的完整應用程序替換了初始代碼,該應用程序再現了問題。這是我能想到的最簡單的例子。
代碼(不相關的部分移除):
<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; }
}
這裏是我的機器上輸出...
您如何驗證實際數據與點擊樹節點不同? – Andy
我目測檢查了調試器中的每個節點,以及在C#2010中使用Mathew MacDonald's Pro WPF中的一個小代碼片段,它實際顯示了TreeView中每個控件的類型和值。 –
如果你所顯示的xaml是完整的,問題是你沒有在'TreeView'上設置'HierarchicalDataTemplate',你正在將模板添加到'TreeView.Items'集合中。我很驚訝它不會拋出關於你設置'ItemsSource'和手動添加項目的例外。相反,模板應該位於UserControl的'Resources'中。 –