views:

114

answers:

2

So I have a TreeView like the one below, with certain items being hidden. If I try to go through the tree using arrow keys, I get stuck at the items surrounding the "collapsed" items. Try it yourself.


<Window x:Class="Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TreeViewBug" Height="300" Width="300">
    <Grid>
        <TreeView>
            <TreeViewItem Header="a." />
            <TreeViewItem Header="b" Visibility="Collapsed" />
            <TreeViewItem Header="c." />
            <TreeViewItem Header="d" />
            <TreeViewItem Header="e." />
            <TreeViewItem Header="f" Visibility="Collapsed"/>
            <TreeViewItem Header="g." />
            <TreeViewItem Header="h" />
        </TreeView>
    </Grid>
</Window>

Nobody else seems to have posted this question (or blogged about it) and so I'm posting it in the hopes of getting a non-hacky solution to this. So here's my question:

How do I fix this bug?

I'm planning on extending the default TreeView to skip items with Visibility set to Hidden/Collapsed or IsEnabled set to false. Has anybody else encountered this before and come up with a good solution?

A: 

I don't understand why this happens. I could fix it by adding:

 IsEnabled="False"

to the collapsed items. Then, navigation works as you'd expect.

Timores
+1  A: 

Here's a solution that works: Disable the item you are hiding. If the item is disabled, keyboard navigation works as it is supposed to. So whenever you set the Visibility of the item, set IsEnabled to false when the item is not visible.

Try the code in the question but add IsEnabled="false" to all the collapsed items. It should work.

apandit