WPF TreeViewItem 切换按钮可见性

发布于 2024-12-12 05:07:01 字数 580 浏览 0 评论 0原文

我遇到了一个问题,希望有人能帮我解决。我遇到过这样的情况:我的节点包含一组可见性设置为 false 的子节点。我希望如果 TreeViewItem 的所有子项都是不可见的,我可以禁用 TreeViewItem 旁边的切换箭头。这可能吗?这是一个示例:

<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200">
    <TreeViewItem Header="Cold Drinks">
        <TreeViewItem Header="Coke" Visibility="False"></TreeViewItem>
        <TreeViewItem Header="Pepsi" Visibility="False"></TreeViewItem>
    </TreeViewItem>
</TreeView>

如何让冷饮 TreeViewItem 隐藏切换箭头?

I've ran into an issue I'm hoping someone can help me solve. I've run into a case where my nodes contain a set of child nodes with visibility set to false. I'm hoping that I can disable the toggle arrow beside the TreeViewItem if all it's children are invisibile. Is this possible? Here's an example:

<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200">
    <TreeViewItem Header="Cold Drinks">
        <TreeViewItem Header="Coke" Visibility="False"></TreeViewItem>
        <TreeViewItem Header="Pepsi" Visibility="False"></TreeViewItem>
    </TreeViewItem>
</TreeView>

How would i get the Cold Drinks TreeViewItem to hide the toggle arrow?

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

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

发布评论

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

评论(2

只有一腔孤勇 2024-12-19 05:07:01

如果您看到 TreeViewItem 的默认 controlTemplate,您将看到切换按钮的可见性绑定到 ItemsControl.HasItems。触发器看起来像这样 -

<Trigger Property="ItemsControl.HasItems">
  <Setter TargetName="Expander" Property="UIElement.Visibility" Value="{x:Static Visibility.Hidden}" />
     <Trigger.Value>
        <s:Boolean>False</s:Boolean>
     </Trigger.Value>
</Trigger>

因此,作为一种解决方法,您可以创建自己的从 TabItem 派生的自定义控件,并将 HasItems 与您自己的 CLR 属性绑定,该属性将循环遍历您的所有 childItems(TreeViewItems),并且如果任何 Item 是,则返回 True如果所有项目都处于隐藏/折叠状态,则为visible或False。这样您的切换按钮将根据触发器自动隐藏。

如果您想知道如何创建自定义控件并将其绑定到 CLR 属性,您可以参考此 -

WPF TreeView 数据绑定以隐藏/显示展开/折叠图标

这与您一直在寻找的内容有些相同。希望这有帮助..

If you see the deafult controlTemplate of TreeViewItem, you will see that visibility of Toggle button is bound to ItemsControl.HasItems. Trigger look like this -

<Trigger Property="ItemsControl.HasItems">
  <Setter TargetName="Expander" Property="UIElement.Visibility" Value="{x:Static Visibility.Hidden}" />
     <Trigger.Value>
        <s:Boolean>False</s:Boolean>
     </Trigger.Value>
</Trigger>

So, as a workaround, you can create your own Custom Control derived from TabItem and bind your HasItems with your own CLR property which will loop through all your childItems(TreeViewItems) and will return True if any of the Item is visible or False if all items are hidden/collapsed state. That way your toggle button will automatically will hide as per Trigger.

In case you want to know how to create Custom control and bind it to your CLR property, you can refer to this -

WPF TreeView databinding to hide/show expand/collapse icon

This is somewhat same what you has been looking for. Hope this helps..

∝单色的世界 2024-12-19 05:07:01

无论是在内部(即键盘导航)还是在其默认模板中,TreeViewItem 都依赖其 HasItems 属性来了解它是否有子项。您可能需要将 TreeViewItem 的 ItemsSource 设置为列表并过滤掉折叠的项目。

Both internally (i.e. for keyboard navigation) and in its default template the TreeViewItem is relying on its HasItems property to know if it has children or not. You will likely need to set the ItemsSource of the TreeViewItem to a list and filter out the collapsed items.

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