【发布时间】:2017-10-04 10:55:28
【问题描述】:
我有一个显示菜单的 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>
【问题讨论】:
-
如果您不想选择项目,为什么希望/期望 SelectedIndex 属性得到更新?
-
我希望能够选择 ListBox 项目,但不希望该项目具有焦点。对于其他项目,它们将导致 MainWindow 布局发生变化,但 ABOUT 项目将打开一个弹出窗口。因此,关注 ABOUT 项没有任何意义。
-
据我所知,Focusable 属性似乎与 IsEnabled 具有相同的功能,因为无法检测到选择了 Focusable 设置为 false 的项目。