添加子节点后,WPF TreeView 节点变得不可选择

发布于 2024-09-26 12:28:28 字数 2298 浏览 1 评论 0原文

我对 WPF TreeView 有一个大问题。我已经设置了一棵与此类似的树。

<TreeView DataContext="{Binding Projects}">
    <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True" />
    </Style>
    <TreeView.Resources>
        <HierarchicalDataTemplate x:Key="LoadTemplate">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="StepTemplate"
                ItemsSource="{Binding Loads}" 
                ItemTemplate="{StaticResource LoadTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="ProjectTemplate"
                ItemsSource="{Binding Steps}"
                ItemTemplate="{StaticResource StepTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
    <TreeViewItem Header="Project Workspace"
            ItemsSource="{Binding}"
            IsExpanded="True"
            ItemTemplate="{StaticResource ProjectTemplate}" />
</TreeView>

TreeView 绑定到一个名为 Projects 的 DependencyProperty,它是 xaml 代码隐藏文件中的 ObservableCollection。树所绑定的类的基本结构遵循树的结构。因此,Project 包含一个实现 INotifyPropertyChanged 和 ICollectionChanged 接口的自定义集合。该集合可通过名为 Steps 的属性访问,并包含 Step 对象。 Step 类还具有可通过名为 Loads 的属性访问的同一自定义集合的实例,并且包含 Load 对象。

现在,当我处理项目中的任何对象时,树的行为会正确,因为节点会随着嵌套集合集的更改而出现和消失并正确更改。在大多数情况下,一切都按其应有的方式进行。

虽然我没有在上面的 Xaml 中显示它,但上面的每个节点都添加了自己的特定上下文菜单。该上下文菜单中的选项之一是添加对象。例如,如果右键单击“步骤”节点,则可以选择在其下方添加“加载”节点。将 Load 类添加到 Step 对象的 Loads 属性中确实会导致节点正常显示在树中。

例如,代码看起来与此类似:

Projects[0].Steps[0].Loads.add(new Load());

好的,这是我长期以来一直试图解决的问题。当新节点出现在树中后,它的父节点就不再可选。因此,在给定的示例中,您无法选择拥有新添加的 Load 节点的 Step 节点。您仍然可以执行 DoubleClick 和 RightClick 等操作并触发事件,但仅尝试用鼠标左键单击不会导致 Step 节点被选择或获得焦点。我已经尝试了我能想到的一切,但我一生都无法弄清楚为什么不。

现在我可以单击树中的其他节点,并且它们可以很好地选择。执行此操作后,之前不允许被选择的节点将恢复其以前的功能并再次能够被选择。

所以发生的情况是,您添加一个新节点,它会显示出来,并且无法选择它的父节点,直到您选择 TreeView 的其他部分,然后一切都会恢复正常。

I have a huge issue with the WPF TreeView. I have setup a tree that is similar to this.

<TreeView DataContext="{Binding Projects}">
    <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True" />
    </Style>
    <TreeView.Resources>
        <HierarchicalDataTemplate x:Key="LoadTemplate">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="StepTemplate"
                ItemsSource="{Binding Loads}" 
                ItemTemplate="{StaticResource LoadTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="ProjectTemplate"
                ItemsSource="{Binding Steps}"
                ItemTemplate="{StaticResource StepTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
    <TreeViewItem Header="Project Workspace"
            ItemsSource="{Binding}"
            IsExpanded="True"
            ItemTemplate="{StaticResource ProjectTemplate}" />
</TreeView>

The TreeView is bound to a DependencyProperty called Projects which is an ObservableCollection in the code-behind file for the xaml. The basic structure of the classes that the tree is bound to follows the structure of the tree. So Project contains a custom collection that implements both the INotifyPropertyChanged and ICollectionChanged interfaces. That collection is accessible through a property called Steps and contains Step objects. The Step class also has an instance of the same custom collection accessable from a property called Loads and contains Load objects.

Now when I work with any object within the Projects, the tree behaves correctly in that nodes appear and dissappear and change correctly as the set of nested collections change. For the most part, everything works as it should.

Althought I didn't show it in the Xaml above, each of the nodes above have their own specific context menu added. One of the options in that context menu is to add an object. So for instance, if you right click on a Step node, you have the option to add a Load node beneath it. Adding the Load class into the Loads property in the Step object does cause a node to show up in the tree just fine.

So for instance the code looks something similar to this:

Projects[0].Steps[0].Loads.add(new Load());

Ok, here is the problem that I've been trying to figure out for the longest time now. After that new node shows up in the tree, it's parent node is no longer selectable. So in the given example, you cannot select the Step node that owns the newly added Load node. You can still do things like DoubleClick and RightClick and get events to fire, but simply attepting to single click with the left mouse button will not cause the Step node to be selected or have focus. I've tried everything I can think of and I cannot for the life of me figure out why not.

Now I can click around on other nodes in the tree and they are selecatable just fine. And after doing so, the node that previously would not allow being selected then regains it's former functionality and is once again able to be selected.

So what happens is that you add a new node, it shows up and it's parent can't be selected until you select some other part of the TreeView and then everything is fine again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-10-03 12:28:28

在没有任何帮助,也没有尝试过任何方法的情况下,我转而使用 Telerik 控件。用 Telerik 的 RadTreeView 替换默认的 TreeView 后,我的所有问题都消失了。我建议人们尽可能避免使用 Microsoft 内置的 WPF TreeView 控件,因为它受到无数问题的困扰,您将花费大量时间尝试处理这些问题,而不是开发应用程序。

With no help and nothing I tried having worked, I switched to using Telerik controls. After replacing the default TreeView with the RadTreeView from Telerik, all my issues disappeared. I'd suggest if possible that people avoid Microsoft's built in WPF TreeView control as it is plagued with countless issues and you'll spend a great amounts of time trying to deal with them, rather than developing your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文