2017-10-04 61 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

相關問題