How to prevent selection of a TreeViewItem based on a condition
Asked Answered
E

2

10

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

Empurple answered 1/2, 2011 at 21:14 Comment(1)
Why not just show a message like "No details available" when selecting those nodes? Making them unselectable it's not very consistent with the functionality of a TreeView. If a user clicks a node and that node does not get selected, his first idea will be that the click was lost (no visual feedback of its action). Not to mention is way easier to implement.Nihhi
D
22

@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>
Diseased answered 25/7, 2011 at 16:34 Comment(1)
Use the following if you want to disable the highlighting on mouse over: <Style TargetType="TreeViewItem"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"/> </Style.Triggers> <Setter Property="Focusable" Value="False" /> </Style>Engadine
D
5

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>
Diaphanous answered 1/2, 2011 at 21:55 Comment(5)
thanks Meleak. It works very well, however I am losing the selection highlight of the last node. Any Idea?Empurple
@jama64: Do you mean that you're able to select it but it doesn't highlight (change color)? In that case, I'm unable to reproduce itDiaphanous
No, it won't select as required but the whole tree loses selection highlight. For example if one of the selectable nodes is active and we try to select one of the unselectable nodes, the unselectable node will not be selected but then the tree losses the selection highlight.Empurple
@jama64: Do the nodes with no details have to be expandable? Otherwise you can make use of IsEnabled. Resetting the TreeView to the last selected item requires some code but I can post that if you'd likeDiaphanous
Meleak, yes the nodes without the detail needs to be expanded and collapsed. they contain the nodes that hav details.Empurple

© 2022 - 2024 — McMap. All rights reserved.