首页 / C# / C#-TreeView不存储预期状态
C#-TreeView不存储预期状态
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-TreeView不存储预期状态,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4239字,纯文字阅读大概需要7分钟。
内容图文
![C#-TreeView不存储预期状态](/upload/InfoBanner/zyjiaocheng/658/3568cbbda1c74718b072c489b554a65e.jpg)
我遇到以下我不理解的行为.我有正确显示在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设置中被继承,因此不妨使用它来节省多余的工作.
内容总结
以上是互联网集市为您收集整理的C#-TreeView不存储预期状态全部内容,希望文章能够帮你解决C#-TreeView不存储预期状态所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。