0

I feel like this should be an easy question, but it doesn't seem to be.

I have a WPF TreeView which consists of buttons:

<TreeView ItemsSource="{Binding Categories}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding ChildCategories}">
            <Button 
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ViewDocumentsCommand}" 
                            CommandParameter="{Binding}" 
                            Style="{DynamicResource TreeButton}"
                            Content="{Binding Description}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

The Button Command loads data into another control on the WPF form. As soon as the user clicks on the content of that control, the button loses focus.However, I want the Button to retain the focussed style so the user is aware which was the last button they clicked.

I have tried noodling about with InactiveSelectionHighlightBrushKey but as far as I know, this isn't valid for a button. How else can I go about this?

EDIT: context was asked for. Effectively, I've used the TreeView to make a mini version of windows explorer. Each node corresponds to a folder, and you can use the expand/collapse button to show sub-folders. Create a sub-folder by right-clicking where you want it in the tree, via a ContextMenu. Click on the folder to see a list of files adjacent to the tree. Clicking on a file causes the folder to lose focus, so the user can't tell which "folder" they're in.

Bob Tway
  • 9,301
  • 17
  • 80
  • 162
  • Would a RadioButton with set groupname be an option instead? So only one is chosen at a time, and would retain "Selected" state after selection. – Chris W. Mar 21 '16 at 15:49
  • @ChrisW. Possibly. I still need to be able to run a command on click, and style it to keep its current look and feel. There's also code I've not included - the buttons have right-click contextmenus too. – Bob Tway Mar 21 '16 at 15:58
  • You can do all of that with a RadioButton also. Realistically it's one of those scenarios where a peek at your whole UX objective would give a better visualization as to what a best proposal would be. – Chris W. Mar 21 '16 at 16:13
  • @ChrisW. Thanks. I have provided some more context. – Bob Tway Mar 21 '16 at 16:35
  • Ooooh, I think I misinterpreted your intention. In that case, what's the button necessary for again? Why not just use TreeViewItem for everything? Like for this style instance just leverage Selected State like [this](http://stackoverflow.com/questions/5047576/wpf-treeview-how-to-style-selected-items-with-rounded-corners-like-in-explorer) – Chris W. Mar 21 '16 at 17:20
  • @ChrisW. Convenience, mostly. In the UI these things look like buttons, and they need to handle a click event. So while I could do this with another control, using a button was the line of least resistance. – Bob Tway Mar 21 '16 at 17:24
  • Yea but TreeViewItem has MouseLeftButtonDown/Up event, which is click. The more I think about it the embedded Button's seem redundant and adding unnecessary objects to the DOM but if you're more familiar with working with Button's I can understand. I'd probably tinker towards a RadioButton styled to look like a Button or whatever you want. – Chris W. Mar 21 '16 at 17:30

0 Answers0