【问题标题】:Detecting a mouse click on a ListBox item with Focusable set to False在 Focusable 设置为 False 的情况下检测鼠标单击 ListBox 项
【发布时间】: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 的项目。

标签: c# wpf listbox


【解决方案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>

【讨论】:

  • 不错的主意。但是之前选择的项目会失去焦点,因此不会出现任何菜单项被选中。我希望当一个项目无法聚焦时,仍然可以检测到对其的点击,但焦点仍会保留在先前选择的项目上。
  • 当你点击另一个元素时它总是会失去焦点。这就是焦点应该如何工作......
  • 我明白了.. 我想在处理“无法聚焦”的项目时,我只需要找到一种方法将所选项目设置回之前选择的项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2010-10-05
相关资源
最近更新 更多