1

I have a ListBox displaying a menu, where the user can navigate in the application. Selecting a ListBox element will display the corresponding UserControl to the right of the menu. However, one of the menu items (ABOUT) should not be focusable, but only carry out a task (open a popup window). The selected item should remain what it was earlier.

Following suggestions in this link I've tried binding the Focusable property of the relevant ListBox element to a boolean property in the VM. But then I don't get any updates on the SelectedIndex property.

Is there any way around this?

ListBox displaying navigation options

XAML:

<ListBox Grid.Row="0" 
            ItemsSource="{Binding MenuButtonInfoList}"
            SelectedIndex="{Binding SelectedMainMenuIndex, Mode=TwoWay}"
            Background="Transparent" BorderBrush="Transparent" Padding="0"
            VerticalContentAlignment="Center">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Focusable" Value="{Binding Path=Focusable}" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Style="{StaticResource MenuButtonBorder}">
                <StackPanel Orientation="Horizontal" Height="Auto">
                    <Image Source="{Binding ImageFilePath}"
                           Style="{StaticResource MenuButtonImage}" />
                    <Label Content="{Binding Label}" 
                           Style="{StaticResource MenuButtonLabel}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Oystein
  • 1,232
  • 11
  • 26
  • You could use [`IsHitTestVisible` from MSDN](https://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible(v=vs.110).aspx) – XAMlMAX Oct 04 '17 at 11:17
  • Why do you want/expect the SelectedIndex property to get updated if you don't want to be able to select the item? – mm8 Oct 04 '17 at 11:49
  • I want to be able to select the ListBox item, but not that the item should have focus. With the other items, they will cause a change in the MainWindow layout, but the ABOUT item will open a popup window. Therefore it doesn't make any sense that the ABOUT item to have focus. – Oystein Oct 04 '17 at 11:53
  • As far as I can see, it seems the Focusable property has the same functionality as IsEnabled, given that one cannot detect selection of an item with Focusable set false. – Oystein Oct 04 '17 at 11:57

1 Answers1

1

You could define a custom ControlTemplate for unfocusable items to make them look like they are not being focused:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Focusable}" Value="False">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" 
                                                BorderThickness="{TemplateBinding BorderThickness}" 
                                                Background="{TemplateBinding Background}" 
                                                Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Not a bad idea. But the previously selected item will lose focus, so no menu items will appear to be selected. I hoped it would be possible that when an item was unfocusable, it would still be possible to detect a click on it but the focus would remain on the previously selected item. – Oystein Oct 04 '17 at 12:41
  • 1
    It will always lose focus when you click on another element. That's how focus is supposed to work... – mm8 Oct 04 '17 at 12:43
  • I see.. I guess I will just have to find a way to set the selected item back to the previously selected item when handling "unfocusable" items. – Oystein Oct 04 '17 at 12:47