我有一个显示菜单的ListBox,用户可以在该菜单中导航。选择ListBox元素将在菜单右侧显示相应的UserControl。但是,其中一个菜单项(ABOUT)不应该是可聚焦的,而只是执行任务(打开一个弹出窗口)。所选项目应保持原样。
在this link中提出以下建议后,我尝试将相关ListBox元素的Focusable属性绑定到VM中的布尔属性。但是,我没有获得SelectedIndex属性的任何更新。
有什么方法吗?
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>
答案 0 :(得分:1)
您可以为不可聚焦的项目定义自定义ControlTemplate
,使它们看起来像是没有聚焦:
<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>