如何防止根据条件选择 TreeViewItem

发布于 2024-10-15 12:18:47 字数 138 浏览 2 评论 0原文

我有 wpf TreeView——绑定到一些数据。 树视图位于窗口的左侧,该窗口分为两个区域,其中树是导航,右侧的面板根据所选的树节点更改内容。

并非树视图的所有节点都会生成详细信息。 我想禁用这些节点的选择。有什么想法吗?

谢谢

I have wpf TreeView -- bound to some data.
The Treeview resides on the left hand of a window divided into two areas where the tree is the navigation and a panel on the right side changes content depending on the tree node selected.

Not all the nodes of the treeview produce detail information.
I want to disable the selection of those nodes. Any idea?

Thanks

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

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

发布评论

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

评论(2

月隐月明月朦胧 2024-10-22 12:18:47

@jama64:如果将样式从 Property IsEnabled 更改为 Focusable,您就可以实现您想要的效果。

<TreeView.ItemContainerStyle>
     <Style TargetType="{x:Type TreeViewItem}">
         <Setter Property="Focusable" Value="{Binding HasDetails}"/>
     </Style>
</TreeView.ItemContainerStyle>

@jama64 : You can achieve what you want if you change the Style from Property IsEnabled to Focusable.

<TreeView.ItemContainerStyle>
     <Style TargetType="{x:Type TreeViewItem}">
         <Setter Property="Focusable" Value="{Binding HasDetails}"/>
     </Style>
</TreeView.ItemContainerStyle>
蹲在坟头点根烟 2024-10-22 12:18:47

您的源代码中是否有类似布尔属性的内容,称为 HasDetails 或其他内容?在这种情况下,你可以使用这样的东西。在 ItemContainerStyle 中创建一个 MultiDataTrigger,该触发器绑定到 DataContext 中的 HasDetailsTreeViewItemIsSelected,并且如果两者都为 True(好吧,HasDetails 为 True)为 False:-),您启动一​​个 Storyboard,“取消选择”新选择的 TreeViewItem

这将禁用所有没有详细信息的 TreeViewItem 的选择,但它们仍然可以展开。希望这就是您正在寻找的

<TreeView ...>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding HasDetails}" Value="False"/>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                Storyboard.TargetProperty="(TreeViewItem.IsSelected)">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </MultiDataTrigger.EnterActions>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

更新

要禁用HasDetails为False的TreeViewItem,您可以使用此

<TreeView ...>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsEnabled" Value="{Binding HasDetails}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Do you have something like a boolean property in your source called HasDetails or something? In that case you can use something like this. Create a MultiDataTrigger in the ItemContainerStyle that binds to HasDetails in the DataContext and IsSelected for the TreeViewItem and if both are True (well, True that HasDetails is False:-), you start a Storyboard that "unselects" the newly selected TreeViewItem.

This will disable selection for all the TreeViewItem's that doesn't have details but they will still be expandable. Hopefully that was what you were looking for

<TreeView ...>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding HasDetails}" Value="False"/>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                Storyboard.TargetProperty="(TreeViewItem.IsSelected)">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </MultiDataTrigger.EnterActions>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Update

To disable the TreeViewItem's where HasDetails is False you can use this

<TreeView ...>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsEnabled" Value="{Binding HasDetails}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文