2017-10-04 63 views
1

我有一个列表框显示菜单,用户可以在应用程序中导航。选择一个ListBox元素将在菜单的右侧显示相应的UserControl。但是,其中一个菜单项(ABOUT)不应该是可以聚焦的,而只能执行一项任务(打开一个弹出窗口)。选定的项目应保持以前的状态。检测鼠标单击列表框项目与焦点设置为False

以下建议this link我已经尝试绑定相关列表框元素的焦点属性到虚拟机中的布尔属性。但是,我没有得到SelectedIndex属性的任何更新。

有没有办法解决这个问题?

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> 
+0

你可以使用['IsHitTestVisible '从MSDN](https://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible(v = vs.110).aspx) – XAMlMAX

+0

为什么你要wan如果您不想选择该项目,可以期待SelectedIndex属性得到更新吗? – mm8

+0

我希望能够选择列表框项目,但不是该项目应该有焦点。对于其他项目,它们将导致MainWindow布局发生变化,但是ABOUT项目将打开一个弹出窗口。因此,关于项目有重点没有任何意义。 – Oystein

回答

1

你可以自定义一个ControlTemplate为unfocusable项目,使它们看起来就像他们没有被关注:

<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> 
+0

不是一个坏主意。但之前选择的项目将失去焦点,因此不会显示菜单项目被选中。我希望有可能在某个项目不可聚焦时,仍然可以检测到它的点击,但焦点仍然会保留在先前选择的项目上。 – Oystein

+1

当你点击另一个元素时,它总是会失去焦点。这就是重点应该如何工作...... – mm8

+0

我明白了..我想我只需要找到一种方法来处理“unfocusable”项目时将选定的项目设置回以前选择的项目。 – Oystein

相关问题